首页 > 试题广场 >

判断闰年

[编程题]判断闰年
  • 热度指数:36124 时间限制: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)
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)
#include <iostream>
using namespace std;

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

发表于 2025-09-09 22:43:39 回复(0)
#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)
import java.util.*;

public class Main{
    public static void main(String[] args){
        Scanner sc=new Scanner(System.in);
        int y=sc.nextInt();
        String is="no";
        if(y%400==0||(y%4==0)&&(y%100!=0)) is="yes";
        System.out.print(is);
    }
}

发表于 2022-07-25 19:11:44 回复(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)
#include <iostream>
using namespace std;

int main() {
    int n;
    cin>>n;
    if(n%400==0||(n%4==0&&n%100!=0)){
        cout<<"yes";
    }
    else{
        cout<<"no";
    }
}
发表于 2025-10-13 22:04:56 回复(0)
#include <stdio.h>

int main() {
    int year = 0;

    scanf("%d", &year);

    if (year <= 0 || year >= 2019) {   //输入限制

        printf("输入的年份超过范围,请重新输入\r\n");

        return -1;

    } else if ((year % 400) == 0) {

        printf("yes");
    }

    else if (((year % 4) == 0 ) && ((year % 100) != 0)) {

        printf("yes");
    }

    else {

        printf("no");

    }

    return 0;
}
发表于 2025-10-12 11:51:17 回复(0)
//你们怎么不对n做取值判定呢,友友
#include <stdio.h>

int main() {
    int n;
    scanf("%d",&n);
    if(n>=1&&n<=2018){ ; }//n符合要求,不做任何处理
    else{
        printf("1≦n≦2018");
    }

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

发表于 2025-09-28 16:35:17 回复(0)
#include <iostream>
using namespace std;

int main() {
    int a;
    cin>>a;
    if((a%4==0)&(a%100!=0)||(a%400==0)) cout<<"yes";
    else cout<<"no";
}
发表于 2025-09-22 12:32:36 回复(0)
#include <iostream>
using namespace std;
int main() {
    int n;
    cin>>n;
    if (n%400==0) {
        cout<<"yes";
    } else {  if(n%4==0){
        if(n%100!=0) {
            cout<<"yes";
       } else cout<<"no";
    } else cout<<"no";
    }
}
发表于 2025-09-16 22:57:51 回复(0)
#include <stdio.h>

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

发表于 2025-09-09 08:55:32 回复(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.println("yes");
            }else {
                System.out.println("no");
            }
        }
    }
}

发表于 2025-08-29 15:47:51 回复(0)
#include <stdio.h>

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

发表于 2025-08-28 21:22:39 回复(0)
#include <stdio.h>

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

发表于 2025-08-20 22:55:08 回复(0)
还是用函数表达比较舒适。
#include <stdio.h>
#include <stdbool.h>

static bool is_leap_year(int n)
{
    if ((n % 400 == 0) ||
        (n % 4 == 0 && n % 100 != 0))
    {
        return true;
    }

    return false;
}

int main() {
    int n = 0;

    scanf("%d", &n);

    printf("%s", is_leap_year(n) ? "yes" : "no");

    return 0;
}


发表于 2025-08-06 17:05:40 回复(0)
n = int(input())
if 1 <= n <= 2018:
    if (n % 4 == 0 and n % 100 != 0) or (n % 400 == 0):
        print("yes")
    else:
        print("no")
else:
    print("请输入一个1<=n<=2018的正整数")
发表于 2025-07-24 10:45:32 回复(0)

问题信息

上传者:牛客301599号
难度:
88条回答 3964浏览

热门推荐

通过挑战的用户

查看代码
判断闰年