求简化!

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

ytezlhy4372 @ 2022-02-03 21:39:39

用了两个void函数,只为main主函数里能简单点…… 求简化的代码如下:

#include<iostream>
#include<cstdio>
using namespace std;
int x,y,s1,s2;
void rn1(int a,int b){
    for(int i=a;i<=b;i++){
        if(i%4==0&&i%100!=0||i%400==0) s1++;
    }
    cout<<s1<<endl;
}
void rn2(int a,int b){
    for(int j=a;j<=b;j++){
        if(j%4==0&&j%100!=0||j%400==0) printf("%d ",j);
    }
}
int main(){
    cin>>x>>y;
    rn1(x,y);
    rn2(x,y);
    return 0;
}

by Escapism @ 2022-02-03 21:51:17

@lvhaoyang9 你可以用数组啊


by Escapism @ 2022-02-03 21:54:53

#include<iostream>
#include<cstdio>
using namespace std;
int x,y,s1,s2;
int year[10000];
void rn1(int a,int b){
    for(int i=a;i<=b;i++){
        if(i%4==0&&i%100!=0||i%400==0) {
            s1++;
            year[s1] = i;
        }
    }
    cout<<s1<<endl;
    for (int i = 1;i <= s1;i++) cout<<year[i]<<" ";
}
int main(){
    cin>>x>>y;
    rn1(x,y);
    return 0;
}

by 违规用户名ddc277a @ 2022-03-21 19:35:29

用数组干啥?

#include <bits/stdc++.h>
using namespace std;
int main() {
    int x, y, sum=0;;
    cin >> x >> y;
    for (int i = x; i <= y; i ++ )
        if(i % 400 == 0 || i % 4 == 0 && i % 100 != 0)
            sum++;
    cout << sum << endl;
    for (int i = x; i <= y; i ++ )
        if(i % 400 == 0 || i % 4 == 0 && i % 100 != 0)
            cout << i << " ";
    cout << endl;
    return 0;
}

|