求大佬指点,提交仅40分,问题出在哪里了呢?

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

Programmer_1745 @ 2022-01-21 20:10:02

//源代码:
#include<bits/stdc++.h>
using namespace std;
int main(){
    int x,y;
    cin>>x>>y;
    cout<<(y-x)/4<<endl;
    //for(int i=x;i<=y;i++){
    while(x<=y){
        if(x%4==0){
            if(x%100==0){
                if(x%400==0){
                    cout<<x<<" ";
                }
            }
            else{
                cout<<x<<" ";
            }
        }
        x++;
    }
    return 0;
} 
//好无语啊,不知道问题在哪儿

by Escapism @ 2022-01-21 20:23:23

@Eric20091204 考虑一下您代码的判断优先级


by Escapism @ 2022-01-21 20:26:53

@Eric20091204 您的优先级:

% 4 == 0 -> % 100 == 0 输出?
      -> % 400 === 0 输出

正确的:


% 4 == 0 -> % 100 == 0 不输出
      -> % 100 != 0 输出
% 400 == 0 -> 输出
```cpp

by Escapism @ 2022-01-21 20:34:16

@Eric20091204 因此,您的代码不能只简单地 / 4来求个数


by Escapism @ 2022-01-21 20:50:57

@Eric20091204 顺便问问,您是小六的吗


by Programmer_1745 @ 2022-01-22 15:18:02

@112358YC 谢谢呀


by Programmer_1745 @ 2022-01-24 12:40:33

@112358YC 哦吼,是的呢(doge)


by biyi_mouse @ 2022-01-31 21:14:51

#include<bits/stdc++.h>

using namespace std;

int main() {
    int x, y, ans = 0;
    vector<int> v;
    cin >> x >> y;
    for (int i = x; i <= y; i++) {
        if ((i % 4 == 0 && i % 100) || i % 400 == 0) {
            v.push_back(i);
            ans++;
        }
    }
    cout << ans << endl;
    for (auto c: v) cout << c << " ";
    return 0;
}

by Programmer_1745 @ 2022-02-03 20:51:53

@yanhaoyang2106 谢谢大佬


by limeng911 @ 2022-04-14 22:06:31

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

int main(){
    int p,q;
    cin>>p>>q;
    int a=p,c=0;
    for(;a<=q;){
        if((a%4==0 && a%100!=0) || a%400==0){
            c=c+1;
        }
        a=a+1;
    }
    cout<<c<<endl;
    a=p ;
    for(;a<=q;){
        if((a%4==0 && a%100!=0) || a%400==0){
            cout<<a<<" ";
            c=c+1;
        }
        a=a+1;
    }
    cout<<endl;
    return 0;
}

你用这个试试,应该可以AC。


|