求助

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

FS_qwq @ 2022-11-16 18:02:07

怎么把sum和i掉换输出呢?

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

by InversionShadow @ 2022-11-16 18:03:44

把i存进一个数组


by kkksc1145 @ 2022-12-10 12:05:46

yuandingquan说的对 是:

#include <iostream>
using namespace std;

int a[4000];

int main()
{
    int x, y, cnt = 0, cur = 0;
    cin >> x >> y;
    // 注:register对代码没影响
    for (register int i = x;i <= y;i++)
    {
        if (i % 4 == 0 && (i % 100 != 0 || i % 400 == 0))
        {
            cnt++;
            cur++;
            a[cur] = i;
        }
    }
    cout << cnt << endl;
    for (int i = 1;i <= cur;i++)
    {
        cout << a[i] << " ";
    }
    return 0;
}

|