40

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

jr001 @ 2024-09-28 21:35:10

#include<bits/stdc++.h>
using namespace std;
int sun = 1, j[110];
bool A(int n){
    if(n%4==0){
        return true;
    }return false;
}
int main (){
    int n,m;
    cin >> n >> m;
    for(int i = n; i<=m; i ++){
        if(A(i)){
            if(i!=0){
                j[sun] = i;
                sun ++;
            }
        }
    }
    cout << sun-1 << "\n";
    for(int  i = 1; i <= sun-1; i++){
        cout << j[i] << " ";
    }
    return 0;
}

by Constantine37 @ 2024-09-28 21:44:58

闰年有一个规则:如果一个年份为100的倍数,这一年就不是闰年;如果一个年份是400的倍数,那么这一年是闰年。你的判断函数中似乎没有涉及这一条的内容。


by Constantine37 @ 2024-09-28 21:47:11

修改后的代码:

#include<bits/stdc++.h>
using namespace std;
int sun = 1, j[110];
bool A(int n){
    if(n%4==0){
        if(n%100==0&&n%400!=0){
            return false;
        }
        return true;
    }return false;
}
int main (){
    int n,m;
    cin >> n >> m;
    for(int i = n; i<=m; i ++){
        if(A(i)){
            if(i!=0){
                j[sun] = i;
                sun ++;
            }
        }
    }
    cout << sun-1 << "\n";
    for(int  i = 1; i <= sun-1; i++){
        cout << j[i] << " ";
    }
    return 0;
}

|