首页 > 试题广场 >

判断闰年

[编程题]判断闰年
  • 热度指数:36242 时间限制: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 整除,因此不是闰年。
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)
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)
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while(scanner.hasNext()){
            int toJudgeLeapYear = scanner.nextInt();
            //1、能被4整除但不能被100整除
            //2、能被400整除
            if(toJudgeLeapYear % 4 == 0 && toJudgeLeapYear % 100 != 0 || toJudgeLeapYear % 400 == 0){
                System.out.println("yes");
            }else{
                System.out.println("no");
            }
      }
    }
}


发表于 2022-06-24 16:05:37 回复(0)
import java.util.*;
public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int x = sc.nextInt();
        if (x % 1000 == 0) {
            System.out.print("yes");
        } else {
            if (x % 4 == 0) {
                if (x % 100 != 0) {
                    System.out.print("yes");
                } else {
                    System.out.print("no");
                }

            } else {
                System.out.print("no");
            }
        }
    }
}

发表于 2022-06-16 16:50:31 回复(0)
import java.util.*;
public class Main{
    public static String getleapyear(int n){
        if(n%4==0&&n%100!=0|n%400==0){
            return "yes";
        }
        else 
            return "no";
    }
    public static void main(String args[]){
        Scanner input=new Scanner(System.in);
        int n=input.nextInt();
        System.out.print(getleapyear(n));
    }
} 

发表于 2022-04-30 16:15:40 回复(0)
import java.util.Scanner;

/**
 *
 * @Title          判断闰年
 * @Description    判断一个整数n是否是闰年
 * 普通闰年:公历年份是4的倍数的,且不是100的倍数,为普通闰年(如2004年、2020年就是闰年)。
 * 世纪闰年:公历年份是整百数的,必须是400的倍数才是世纪闰年(如1900年不是世纪闰年,2000年是世纪闰年)
 * 输入描述:
 *      输入一个整数n (1≤n≤2018)
 * 输出描述:
 *      是闰年输出"yes" 否则输出"no"
 * @author weijunfu<ijunfu @ qq.com>
 * @date 2022/03/16 10:51
 * @version 1.0.0
 *
 */

public class Main {

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int i = in.nextInt();

        if(i%400 == 0 || (i%100!=0 && i%4==0)) {
            System.out.println("yes");
        } else {
            System.out.println("no");
        }

        in.close();
    }
}

发表于 2022-03-16 10:54:40 回复(0)