ง่าย 0 ข้อที่แก้แล้ว อัตราผ่าน 0%

Season From Month and Day

Given a month name and a day, return the season of the year in the Northern Hemisphere.

สิ่งที่คุณจะได้เรียนรู้

  • Validate a month name and day before using them
  • Turn a month name into a month number
  • Compare (month, day) pairs against range boundaries

โจทย์

ดูฉบับแปล

A weather club wants a small helper for its sign-up form: members type the month of their birthday as a word (for example October) and the day as a number, and the form shows the season they were born in. The club uses these fixed dates for the Northern Hemisphere:

  • Spring: March 20 to June 20
  • Summer: June 21 to September 21
  • Autumn: September 22 to December 20
  • Winter: December 21 to March 19

All ranges include both end dates.

Implement season_from_date(month, day).

Input

month: The month as a full English name with a capital first letter, such as "March". Any other text ("march", "Mar", "") is invalid.

day: The day of the month. It is valid from 1 up to the number of days in that month; February allows 29.

Output

Return "Spring", "Summer", "Autumn" or "Winter". If the month name or the day is not valid, return "Invalid".

เงื่อนไข

month has at most 20 characters

−1000 ≤ day ≤ 1000

ตัวอย่าง

ตัวอย่างที่ 1
อินพุต:
month = "April"
day = 11

ผลลัพธ์ที่คาดหวัง:
expected = "Spring"
อินพุต:
std::string month = "April";
long long day = 11;

ผลลัพธ์ที่คาดหวัง:
std::string expected = "Spring";
อินพุต:
string month = "April";
long day = 11;

ผลลัพธ์ที่คาดหวัง:
string expected = "Spring";

คำอธิบาย

April 11 falls between March 20 and June 20, so the answer is "Spring".

ตัวอย่างที่ 2
อินพุต:
month = "February"
day = 30

ผลลัพธ์ที่คาดหวัง:
expected = "Invalid"
อินพุต:
std::string month = "February";
long long day = 30;

ผลลัพธ์ที่คาดหวัง:
std::string expected = "Invalid";
อินพุต:
string month = "February";
long day = 30;

ผลลัพธ์ที่คาดหวัง:
string expected = "Invalid";

คำอธิบาย

February never has 30 days, so the date is "Invalid".

แนวทางการแก้

ลองทำเองก่อน แล้วค่อยเปิดคำใบ้ทีละข้อ

คำใบ้ 1

Check that the month is one of the twelve names and that the day fits that month first.

คำใบ้ 2

Comparing month first and day second lets you test whether a date is inside a range.

คำใบ้ 3

Winter wraps around the new year, so handle it as whatever is left after the other three seasons.

ความซับซ้อนที่คาดหวัง

O(1) time, O(1) extra space

แหล่งอ้างอิง

โจทย์นี้ปรับให้เหมาะกับ CodeUtility กรุณาทำตามคำชี้แจงและข้อจำกัดด้านบน

คำตอบที่ผ่านดีที่สุดของแต่ละคน คำตอบที่เร็วกว่า ใช้หน่วยความจำน้อยกว่า และได้คะแนนดีกว่าจะอยู่ด้านบน

ยังไม่มีคำตอบที่ผ่าน แก้โจทย์นี้แล้วคว้าอันดับหนึ่งไปเลย

เรียนรู้จากคนอื่น

แนวทางจากชุมชน

ยังไม่มีวิธีทำที่เผยแพร่ ลองแก้โจทย์นี้เพื่อแชร์วิธีแรก