Tất cả thử thách
Dễ 0 đã giải Tỷ lệ chấp nhận 0%

Season From Month and Day

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

Bạn sẽ học được gì

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

Đề bài

Xem bản dịch

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".

Ràng buộc

month has at most 20 characters

−1000 ≤ day ≤ 1000

Ví dụ

Ví dụ 1
Đầu vào:
month = "April"
day = 11

Kết quả mong đợi:
expected = "Spring"
Đầu vào:
std::string month = "April";
long long day = 11;

Kết quả mong đợi:
std::string expected = "Spring";
Đầu vào:
string month = "April";
long day = 11;

Kết quả mong đợi:
string expected = "Spring";

Giải thích

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

Ví dụ 2
Đầu vào:
month = "February"
day = 30

Kết quả mong đợi:
expected = "Invalid"
Đầu vào:
std::string month = "February";
long long day = 30;

Kết quả mong đợi:
std::string expected = "Invalid";
Đầu vào:
string month = "February";
long day = 30;

Kết quả mong đợi:
string expected = "Invalid";

Giải thích

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

Hướng tiếp cận

Hãy tự thử trước, sau đó mở từng gợi ý một.

Gợi ý 1

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

Gợi ý 2

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

Gợi ý 3

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

Độ phức tạp mong đợi

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

Tham khảo

Thử thách này đã được điều chỉnh cho CodeUtility. Hãy làm theo đề bài và các ràng buộc ở trên.

Lời giải đạt tốt nhất của mỗi người. Lời giải nhanh hơn, tốn ít bộ nhớ hơn và được đánh giá cao hơn sẽ đứng trên.

Chưa có lời giải nào đạt. Giải bài này để giành hạng nhất.

Học hỏi từ người khác

Giải pháp cộng đồng

Chưa có giải pháp nào được chia sẻ. Hãy giải thử thách này để đăng giải pháp đầu tiên.