songboqian @ 2023-12-18 13:51:15
#include<bits/stdc++.h>
using namespace std;
int x,y,a,b[1000];
void input(){
cin>>x>>y;
}
void print(){
for(int i=x;i<=y;i++){
if(i%4==0&&i%100!=0){
a+=1;
b[a]+=i;
}else if(i%4==0){
a+=1;
b[a]+=i;
}
}
cout<<a<<endl;
for(int j=1;j<=a;j++){
cout<<b[j]<<" ";
}
}
int main(){
input();
print();
return 0;
}
by wumingwei @ 2023-12-18 13:54:33
@songboqian 1700,1800,1900...等都不是闰年
by ys_kylin__ @ 2023-12-18 13:56:58
@songboqian
#include<bits/stdc++.h>
using namespace std;
int x,y,a,b[1000];
void input(){
cin>>x>>y;
}
void print(){
for(int i=x;i<=y;i++){
if(i%4==0&&i%100!=0){
b[++a]=i;
}
else if(i%400==0) {
b[++a]=i;
}
}
cout<<a<<endl;
for(int j=1;j<=a;j++){
cout<<b[j]<<" ";
}
}
int main(){
input();
print();
return 0;
}
by songboqian @ 2023-12-18 13:57:23
我的程序也认定它们不是闰年,我试过了
by xiaoshumiao @ 2023-12-18 14:49:16
@songboqian
else if(i%4==0){
a+=1;
b[a]+=i;
}
改成:
else if(i%400==0){
a+=1;
b[a]+=i;
}
另外数组开小了。
by xiaoshumiao @ 2023-12-18 14:49:48
数组没开小,抱歉。
by ys_kylin__ @ 2023-12-19 12:43:27
@songboqian 你的问题在于四百年的判断有误,不是 else if(i%4==0)
而是 else if(i%400==0)
,剩下不用改,我之前的那个程序就是改完的(AC了)
by songboqian @ 2023-12-19 12:43:35
AC了,感谢各路网友的讲解!!!!!
by songboqian @ 2023-12-19 12:45:53
@ys_kylin__ (^__^) 嘻嘻
by shimucheng @ 2023-12-29 21:09:35
你还差了一个判断,在整百年的时候还需要%400,判断能否被400整除
by Sci_8633 @ 2024-01-12 22:31:49
@songboqian 实在不行就不要用自定义函数。
#include<bits/stdc++.h>
using namespace std;
int x,y,sum;
bool h[3001];
int main(){
cin>>x>>y;
for(int i=x;i<=y;++i)
if(i%400==0){
sum++;
h[i]=1;
}else if(i%100==0){
h[i]=0;
}else if(i%4==0){
sum++;
h[i]=1;
}
cout<<sum<<endl;
for(int i=x;i<=y;++i)
if(h[i]==true)
cout<<i<<' ';
return 0;
}