40分……

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

ASOUL_Official @ 2020-08-01 10:13:54

评测记录

#include<iostream>
using namespace std;
int main(){
    int kaishi,jieshu,i,m=0; //m:计数器
    cin>>kaishi>>jieshu;
    for(i=kaishi;i<=jieshu;i++){
        if(i%4==0||(i%400==0&&i%100==0)){
            m++;
        }
    }
    cout<<m<<endl;
    for(i=kaishi;i<=jieshu;i++){
        if(i%4==0||(i%400==0&&i%100==0)){
            cout<<i<<" ";
        }
    }
    return 0;
}

by qian_shang @ 2020-08-01 10:16:50

@lancdn 您if里面写错了应该是:

if(i%400==0||(i%4==0&&i%100!=0))

by ⚡zhangjingcan⚡ @ 2020-08-01 10:17:23

@lancdn i%100!=0


by CiCi_Qrt @ 2020-08-01 17:04:13

@lancdn

评测记录

完整代码

#include<iostream>
using namespace std;
int main(){
    int kaishi,jieshu,i,m=0; //m:计数器
    cin>>kaishi>>jieshu;
    for(i=kaishi;i<=jieshu;i++){
        if((i%4==0&&i%100!=0)||(i%400==0&&i%100==0)){
            m++;
        }
    }
    cout<<m<<endl;
    for(i=kaishi;i<=jieshu;i++){
        if((i%4==0&&i%100!=0)||(i%400==0&&i%100==0)){
            cout<<i<<" ";
        }
    }
    return 0;
}

注意闰年判断的时候%4==0时要判断是否%100!=0

举个栗子

1900不是闰年 但是1900%4==0

所以要把

if(i%4==0||(i%400==0&&i%100==0))

改为

if((i%4==0&&i%100!=0)||(i%400==0&&i%100==0))

by ASOUL_Official @ 2020-08-02 14:59:32

@长郡的屑同学 @zhangjingcan @qian_shang 谢过诸位


|