C语言60分求助

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

EkSulfur @ 2023-08-18 22:56:56

#include <stdio.h>
int yn(int x);
void run(int x, int y);
int total(int x, int y);

int main(void) {
    int x, y;
    scanf("%d%d", &x, &y);
    printf("%d\n", total(x, y));
    run(x, y);

    return 0;
}

int yn(int x) {
    int a;
    if (x % 4 == 0) {
        if (x % 100 == 0 && x % 1000 != 0)
            a = 0;
        else
            a = 1;
    } else
        a = 0;
    return a;
}

int total (int x, int y) {
    int t = 0;
    for (int i = x; i <= y; i++) {
        if (yn(i) == 1)
            t++;
    }
    return t;
}

void run(int x, int y) {
    for (int i = x; i <= y; i++) {
        if (yn(i) == 1)
            printf("%d ", i);
    }
}

by Use_Imagination @ 2023-08-18 23:12:01

@EkSulfur yn函数写成这样:

    int a=0;
    if (x % 4 == 0&&x%100!=0||x%400==0)
        a =!a;
    return a;

by OIerWu_829 @ 2023-08-18 23:15:16

#include <bits/stdc++.h>
using namespace std;

int main()
{
    int x, y;
    cin >> x >> y;
    int cnt = 0;
    for (int i = x; i <= y; i++)
    {
        if (i % 400 == 0 || i % 100 != 0 && i % 4 == 0)
        {
            cnt++;
        }
    }
    cout << cnt << endl;
    for (int i = x; i <= y; i++)
    {
        if (i % 400 == 0 || i % 100 != 0 && i % 4 == 0)
            cout << i << " ";
    }
    return 0;
}

by FurippuWRY @ 2023-08-19 00:45:19

@EkSulfur 可以看看我的

#include<bits/stdc++.h>

using namespace std;

bool r(long long i){

    if(i%4==0 && i%100!=0 || i%400==0 && i%3200!=0) return 1;

    else return 0;

}

int main()

{

    ios::sync_with_stdio(false);

    cin.tie(0), cout.tie(0);

    long long x,y,a=0;

    cin>>x>>y;

    for(int i=x;i<=y;++i){

        if(r(i)) a++;

    }

    cout<<a<<endl;

    for(int j=x;j<=y;++j){

        if(r(j)) cout<<j<<" ";

    }

    return 0;

}

by EkSulfur @ 2023-08-21 18:42:36

感谢


|