Tutte le sfide
Facile 7 risolte 36.8% di accettazione

Days in a Month

Return how many days a month has, including leap years.

Cosa imparerai

  • Map months to day counts
  • Reuse a leap-year rule
  • Split logic into small functions

Problema

Mostra traduzione

Return the number of days in month of year in the Gregorian calendar. February has 29 days in a leap year: years divisible by 4, except those divisible by 100 unless also divisible by 400. A helper function is_leap(year) keeps the code tidy.

Implement days_in_month(year, month).

Input

year: The year. month: The month, 1 (January) to 12 (December).

Output

Return the number of days.

Vincoli

1 ≤ year ≤ 9,999; 1 ≤ month ≤ 12

Esempi

Esempio 1
Input:
year = 2024
month = 2

Output atteso:
expected = 29
Input:
long long year = 2024;
long long month = 2;

Output atteso:
long long expected = 29;
Input:
long year = 2024;
long month = 2;

Output atteso:
long expected = 29;

Spiegazione

2024 is divisible by 4 and not by 100, so it is a leap year: 29 days.

Esempio 2
Input:
year = 1900
month = 2

Output atteso:
expected = 28
Input:
long long year = 1900;
long long month = 2;

Output atteso:
long long expected = 28;
Input:
long year = 1900;
long month = 2;

Output atteso:
long expected = 28;

Spiegazione

1900 is divisible by 100 but not 400, so it is not a leap year: 28 days.

Come affrontarlo

Prova prima da solo, poi apri un suggerimento alla volta.

Suggerimento 1

Most months have a fixed day count; February depends on whether the year is leap.

Suggerimento 2

Use a small lookup for month lengths, then adjust February with the leap-year rule.

Suggerimento 3

Be careful with century years: divisible by 100 is not enough unless also divisible by 400.

Complessità attesa

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

Riferimenti

Questa sfida è adattata per CodeUtility. Segui la traccia e i vincoli sopra.

La migliore soluzione superata di ogni programmatore. Le più veloci, leggere e meglio valutate stanno in alto.

# Programmatore Linguaggio Tempo Memoria Voto dell'IA Soluzione
1 Н"Никита "Programmer" Python 794 ms - 5/5 Vedi il codice
2 HJhettory Jurash Python 828 ms - 4/5 Vedi il codice
3 LMLaura Mahlow Python 843 ms - 4/5 Vedi il codice
4 ゼゼロダニ Python 845 ms - 4/5 Vedi il codice
5 KKKaneki Ken Python 866 ms - 4/5 Vedi il codice
6 ·𝓕·.★·.·⍣·.·★ 𝓕𝓪𝓫𝓲𝓸𝓵𝓪 𝓔𝓵𝓮𝓷𝓪 ★·.·⍣·.·★.· C++ 3,167 ms - 4/5 Vedi il codice
7 PTphúc thịnh phạm C++ 3,331 ms - 4/5 Vedi il codice

Impara dagli altri

Soluzioni della community

Attenzione spoiler: 7 soluzioni complete sotto Prova prima a risolverla da solo. Puoi comunque sbirciare il codice degli altri sviluppatori e le revisioni dell'IA. Accedi e risolvi la sfida per partecipare alla discussione.