Fácil 2 resolvidos 66.7% de aceitação

Leap Year

Check whether a year follows the Gregorian leap-year rule.

O que você vai aprender

  • Combine divisibility rules with and/or
  • Apply the century exception
  • Write a clear one-line condition

Problema

Ver tradução

A year divisible by 4 is a leap year unless it is also divisible by 100. Years divisible by 400 are leap years despite that exception. Apply this arithmetic rule to every allowed input year.

Implement leap_year(year).

Input

year: The year to check.

Output

Return true for a leap year and false otherwise.

Restrições

1 ≤ year ≤ 9999.

Exemplos

Exemplo 1
Entrada:
year = 2024

Saída esperada:
expected = True
Entrada:
long long year = 2024;

Saída esperada:
bool expected = true;
Entrada:
long year = 2024;

Saída esperada:
bool expected = true;

Explicação

2024 is divisible by 4 and not by 100.

Exemplo 2
Entrada:
year = 2100

Saída esperada:
expected = False
Entrada:
long long year = 2100;

Saída esperada:
bool expected = false;
Entrada:
long year = 2100;

Saída esperada:
bool expected = false;

Explicação

2100 is divisible by 100 but not by 400, so the century exception applies.

Como abordar

Tente sozinho primeiro e depois abra uma dica de cada vez.

Dica 1

Most years follow the divisible-by-4 rule, but centuries are exceptions.

Dica 2

A year divisible by 400 is still a leap year, even though it is also divisible by 100.

Dica 3

Check the exception before rejecting century years, or you may misclassify years like 2000.

Complexidade esperada

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

Referências

Este desafio foi adaptado para o CodeUtility. Siga o enunciado e as restrições acima.

A melhor solução aprovada de cada programador. As mais rápidas, leves e mais bem avaliadas ficam no topo.

# Programador Linguagem Tempo Memória Avaliação da IA Solução
1 KKKaneki Ken Python 1,006 ms - 5/5 Ver código
2 GTGarlic The Destroyer Rust 1,455 ms - 5/5 Ver código

Aprenda com outros

Soluções da comunidade

Atenção: 2 soluções completas abaixo Tente resolver sozinho primeiro. Você ainda pode espiar o código de outros desenvolvedores e as avaliações da IA. Entre e resolva o desafio para participar da discussão.