洛谷 P5737 【深基7.例3】闰年展示

思路

用一个函数来判断是否是闰年,然后主函数一个循环判断就行。

代码

#include <bits/stdc++.h>
using namespace std;
vector<int> V;
bool judgeYear(int x) {
 if ((x % 4 == 0 && x % 100 != 0) || x % 400 == 0) {
 return true;
 }
 return false;
}
int main() {
 freopen("in.txt", "r", stdin);
 int a, b;
 int sum=0;
 cin >> a >> b;
 for (int k = a; k <= b; k++) {
 if (judgeYear(k)) {
 V.push_back(k);
 sum++;
 }
 }
 cout<<sum<<endl;
 for (auto i:V){
 cout<<i<<" ";
 }
 return 0;
}

多多
1 声望0 粉丝