高质量题解|A7855.闰年
2026-02-17 13:49:33
发布于:河北
0阅读
0回复
0点赞
解题思路
主函数内判断的内容已经写好了,我们要写的就是 is_leap 这个函数内的判断闰年
闰年的判断是能被 4 整除,不能被 100 整除,或者能被 400 整除
根据这个说法我们就能推算出判断的条件是
n % 4 == 0 && n % 100 != 0 || n % 400 == 0
如果可以,那么就 return true,不可以就 return false
这里是 bool 类型的函数,也可以是 int ,但是就要 return 0 或 1
代码
#include<iostream>
using namespace std;
// 完成闰年判定 is_leap 函数
bool is_leap(int n){
if(n % 4 == 0 && n % 100 != 0 || n % 400 == 0){
return true;
}else{
return false;
}
}
int main() {
int n;
cin >> n;
if(is_leap(n)) cout << "YES" << endl;
else cout << "NO" << endl;
return 0;
}
这里空空如也








有帮助,赞一个