各位大救救我,3号点对其他错了!输出还有点不对!

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

CJKZS @ 2023-06-12 19:52:38

#include<bits/stdc++.h>
using namespace std;
int main (){
    int x,y,a[1010],k=0;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
        if((i%400==0||i%4==0)&&i%100!=0){
            k++;
            a[k]=i;
        }
    } 
    cout<<k<<"\n";
    for(int i=1;i<=k;i++){
        cout<<a[i]<<" ";
    } 
    return 0;
}

by CznTree @ 2023-06-12 19:59:18

@CJZS 这样判闰年可能会好debug一些:

if(i%100==0&&i%400==0) 
        {
            cout<<i<<' ';
        }
        else if(i%100==0&&i%400!=0) continue;
        else if(i%100!=0&&i%4==0) 
        {
            cout<<i<<' ';
        }

by Trump__Biden @ 2023-06-12 20:32:14

@CJZS

#include<bits/stdc++.h>
using namespace std;
int main (){
    int x,y,a[1010],k=0;
    cin>>x>>y;
    for(int i=x;i<=y;i++){
        if((i%100!=0&&i%4==0)||i%400==0){
            k++;
            a[k]=i;
        }
    } 
    cout<<k<<"\n";
    for(int i=1;i<=k;i++){
        cout<<a[i]<<" ";
    } 
    return 0;
}

by CJKZS @ 2023-06-12 20:53:13

谢谢各位大佬!我懂了!


by C_chenxuyao @ 2023-07-18 22:07:23

@CJZS 改成这样会更好一些:


//writer:chenxuyao
#include<bits/stdc++.h>
using namespace std;
int a[10001];
int js,s=1;
int main()
{
    int x,y;
    cin>>x>>y; 
    for(int i=x;i<=y;i++){
        if(i%4==0&&i%100!=0||i%400==0){
            js++;
            a[s]=i;
            s++;
        }
    }
    cout<<js<<endl;
    for(int i=1;i<=js;i++)
        cout<<a[i]<<" ";
    return 0;
}

|