首页 > 试题广场 >

判断闰年

[编程题]判断闰年
  • 热度指数:25678 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 256M,其他语言512M
  • 算法知识视频讲解
\hspace{15pt}给定一个整数 n,判断其是否为闰年。闰年的判定规则如下:
\hspace{23pt}\bullet\,如果 n 能被 400 整除,则为闰年;
\hspace{23pt}\bullet\,否则如果 n 能被 4 整除且不能被 100 整除,则为闰年;
\hspace{23pt}\bullet\,否则,不是闰年。

输入描述:
\hspace{15pt}在一行中输入一个整数 n,满足 \left(1 \leqq n \leqq 2018\right)


输出描述:
\hspace{15pt}输出一个字符串,若 n 为闰年,输出 "yes"(不含双引号);否则输出 "no"(不含双引号)
示例1

输入

2000

输出

yes

说明

2000 能被 400 整除,因此是闰年。
示例2

输入

1900

输出

no

说明

1900 能被 100 整除但不能被 400 整除,因此不是闰年。
#include<iostream>
using namespace std;

int main()
{
    int n;
    cin >> n;
    if (n % 400 == 0 || n % 4 == 0 && n % 100 != 0) cout << "yes" << endl;
    else cout << "no" << endl;
}

发表于 2022-02-26 14:07:32 回复(2)
#include<bits/stdc++.h>
using namespace std;
int main(){
    int a;
    cin >>a;
    if(a%4==0 && a%100!=0){
        cout<<"yes"<<endl;
           
    }
    else if(a%400==0){
        cout<<"yes"<<endl;
    }
    else{
        cout<<"no"<<endl;
    }
    return 0;
}
发表于 2024-11-25 18:48:51 回复(0)
//能被4整除但不能被100整除的年份是闰年,能被400整除的年份也是闰年。
//2024年是闰年,因为它可以被4整除,但不能被100整除。
//1900年不是闰年,因为它可以被100整除,但不能被400整除。
//1600年是闰年,可以被4整除,也能被100整除,但它同时也能被400整除。

#include <stdio.h>

int main()
{
    int a = 0;
    scanf("%d", &a);
    
    if(a % 400 == 0)
    {
        printf("yes\n");
    }
    else
    {
        if(a % 4 == 0 && a % 100 != 0)
        {
            printf("yes\n");
        }
        else {
            printf("no\n");
        }
    }
    return 0;
}

发表于 2024-01-28 21:56:09 回复(0)
#include <stdio.h>
//闰年的判断方法可以简化为:
// 1. 能被4整除但不能被100整除的年份是闰年
// 2. 能被400整除的年份也是闰年
int main() {
    int n=0;
    scanf("%d",&n);
    if((n%4==0&&n%100!=0)||(n%400==0))
    {
        printf("yes");
    }
    else {
    {
        printf("no");
    }
    }
    return 0;
}

发表于 2024-01-13 10:15:36 回复(0)
n=int(input())
if (n%4==0 and n%100!=0)&nbs***bsp;n%400==0:
    print('yes')
else:
    print('no')    

发表于 2023-01-18 09:44:35 回复(0)
year= int(input())
if year % 400 == 0:
    print('yes')
elif (year % 4 == 0) and (year % 100 != 0):
    print('yes')
else:
    print('no')
发表于 2022-03-17 19:16:16 回复(1)
n = int(input())

if n % 400 == 0:
    print ("yes")
elif (n % 4 == 0) & (n % 100 !=0):
    print ("yes")
else:
    print("no")
发表于 今天 05:50:48 回复(0)
n = int(input())
print('yes' if n % 400 == 0 or (n % 4 ==0 and n %100 != 0) else 'no' )
发表于 2025-07-10 09:59:47 回复(0)
java 有手就行
import java.util.Scanner;
import java.time.LocalDate;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner src = new Scanner(System.in);
        int a = src.nextInt();
        LocalDate ld = LocalDate.of(a,3,1);




        LocalDate ld2 = ld.minusDays(1);
        int day = ld2.getDayOfMonth();
        if(day == 29){
            System.out.print("yes");
        }
        else{
            System.out.print("no");
        }
    }

}

发表于 2025-07-06 01:00:44 回复(0)
#include <stdio.h>

int main() {
    int n;
    while(1){
        if(scanf("%d",&n)!=1){
            while(getchar()!='\n');
            continue;
        }
        while(getchar()!='\n');
        if(n < 1 || n > 2018){
            continue;//如果输入错误就重新开始while的循环,直到正确.
        }
        if((n % 400 == 0) || (n % 4 == 0 && n % 100 != 0)){
            printf("yes\n");
        }else{
            printf("no\n");
        }
        break;
    }
    return 0;
}

发表于 2025-07-01 18:02:11 回复(0)
#include <stdio.h>

int main() {
    int n;
    scanf("%d", &n);
    if(n%400==0||n%4==0&&n%100!=0)
    {
        printf("yes");
    }
    else {
        printf("no");
    }
    return 0;
}
发表于 2025-06-25 19:46:45 回复(0)
n = int(input())

if n%400==0:
    print("yes")
elif n%4==0 and n%100 != 0:
    print("yes")
else:
    print("no")
发表于 2025-06-17 16:48:32 回复(0)
#include <stdio.h>

int main()
 {
    int n = 0;
    scanf("%d", &n);

    if (n % 400 == 0)
    {
        printf("yes\n");
    }
    else if ( n % 4 == 0 && n % 100 != 0)
    {
        printf("yes\n", n);
    }
    else
    {
        printf("no\n");
    }

    return 0;
}
发表于 2025-06-08 20:58:39 回复(0)
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNextInt()) { // 注意 while 处理多个 case
            int a = in.nextInt();
            if(a % 400 == 0 || (a % 4 ==0)&&(a % 100 !=0)){
                System.out.printf("yes");
            }else{
                System.out.printf("no");
            }
        }
    }
}
发表于 2025-06-06 15:11:09 回复(0)
n = int(input())
if n % 4 == 0 and n % 100 != 0 or n %400 == 0:
    print('yes')
else:
    print('no')
发表于 2025-05-27 09:27:59 回复(0)
a = int(input())
if (a%4==0 and a%100!=0)&nbs***bsp;(a%400==0):
    print("yes")
else:
    print("no")

发表于 2025-04-28 10:37:27 回复(0)
a=int(input())
if (a%4==0 and a%100!=0) or (a%400==0):
    print('yes')
else:
    print('no')

闰年分为普通闰年和世纪闰年,具体判断规则如下:
  • 普通闰年:公历年份是 4 的倍数,且不是 100 的倍数的年份为普通闰年。例如 2004 年能被 4 整除且 不能被 100 整除,所以 2004 年是普通闰年。
  • 世纪闰年:公历年份是 400 的倍数的年份为世纪闰年。例如 2000 年能被 400 整除,所以 2000 年是世纪闰年;而 1900 年不能被 400 整除,虽然它能被 4 整除,但它同时能被 100 整除,所以 1900 年不是闰年。

发表于 2025-02-14 17:31:40 回复(0)
#include <stdio.h>

int main()
{
    int n;
    scanf("%d",&n);
    if ((n%4==0&&n%100!=0)||n%400==0)
    {
        printf("yes");
    }
    else
    {
        printf("no");
    }
    return 0;
}
发表于 2024-11-25 20:59:56 回复(0)
#include <stdio.h>

int main() {
    int a;
    scanf("%d", &a);
    if ((a % 4 == 0 ) && (a % 100 != 0) || (a % 400 == 0 ) ) {
        printf("yes\n");
    } else {
        printf("no\n");
    }
    return 0;
}
#include <stdio.h>

int main() {
    int a;
    scanf("%d", &a);
    if ((a % 4 == 0 ) && (a % 100 != 0) || (a % 400 == 0 ) ) {
        printf("yes\n");
    } else {
        printf("no\n");
    }
    return 0;
}

发表于 2024-10-03 21:10:31 回复(0)
#include <stdio.h>

int main() {
    int n;
    scanf("%d",&n);
    if(1<=n&&n<=2018)
    {
        if((n%4==0&&n%100!=0)||n%400==0)
        {
            printf("yes\n");
        }
        else
        {
            printf("no\n");
        }
    }
    return 0;
}

发表于 2024-09-24 20:38:21 回复(0)