判断闰年

GitHub跳转原题关系图返回列表

按 4、100、400 和题设 3200 的整除规则判断闰年。

OJ: noi_openjudge

题目 ID: ch0104-17

难度:入门

标签:数学条件判断python

日期: 2026-07-30 23:01

题意

按题设公历规则判断年份是否是闰年,输出 YN

思路

普通规则是“能被 4 整除且不能被 100 整除,或能被 400 整除”。题面额外规定 3200 的倍数不是闰年,因此最后再加 year % 3200 != 0

代码

Python代码

python
year = int(input())
is_leap = year % 4 == 0 and (year % 100 != 0 or year % 400 == 0) and year % 3200 != 0
print("Y" if is_leap else "N")

C++代码

cpp
#include <cstdio>
int main(){
	int a,b,c;
	scanf("%d",&a);
	if(  a % 4 == 0){
		if( a % 100 ==0 && a % 400 != 0 )
			printf("N");
		else
			printf("Y");
	}
	else
		printf("N");
	return 0;
}

复杂度

时间复杂度和额外空间复杂度均为 O(1)O(1)

总结

闰年题的难点在例外规则;用带括号的布尔表达式把优先级写清楚。