Winterblessed @ 2024-02-18 13:19:37
#include <iostream>
#include <vector>
using namespace std;
bool is(int year) {
if ( (year % 400 == 0) || (year % 100 != 0
&& year % 4 == 0) )
return true;
else return false;
}
vector<int> arr;
int main() {
int a,b;
cin >> a >> b;
for(int i = a; ; i++) {
if(is(i)) {
a = i;
break;
}
}
for(int i = a; i <= b; i+=4) {
arr.push_back(i);
}
cout << arr.size()<<'\n';
for(auto item:arr) {
cout << item << ' ';
}
return 0;
}
by Handezheng @ 2024-02-18 13:32:55
@Winterblessed
每四年塞入vector里面
如果这个第4年正好可以被100整除但是不可以被400整除呢?
by __Rickysun__ @ 2024-02-18 13:34:17
@Winterblessed 你有没有想过如果是整百数年,你+4,就错了,所以我觉得应该加个特判(我觉得你还不如直接全部循环遍历,数据量特别小,不会超时的)
by __Rickysun__ @ 2024-02-18 13:35:57
@Winterblessed 因为如果你加上特判后复杂度其实还比循环遍历高了
by Winterblessed @ 2024-02-18 14:13:50
@Handezheng 明白了,谢谢
by Winterblessed @ 2024-02-18 14:15:31
@Rickysun 明白了,谢谢 (。・ω・。)