C为什么我的显示编译错误啊大佬们,自己的编译器可以正确跑

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

elong123 @ 2023-11-08 21:47:53


#include<stdio.h>
#include<stdbool.h>
bool SelectYear(int x){
    if(x % 100 != 0 && x % 4 == 0 || x % 400 == 0){
        return true;
    }
    return false;
}
int main(){
    int x,y;
    int i = 0;
    int a[2000];
    scanf("%d %d",&x,&y);
//  bool s = SelectYear(x);
    while(x < y){
        if(SelectYear(x)){
            a[i] = x;
            i++;
        }
        x++;        
    }
    printf("%d\n",i);
    for(int j = 0; j < i; j++){
        printf("%d ",a[j]);
    }
    return 0;
}

by suyulong1212 @ 2023-11-08 21:52:36

大概是SelectYear函数那里||和&&逻辑混乱


by _____QWQ_____ @ 2023-11-08 21:57:25

看着是没有啊 @suyulong1212

可以把while 换成 for试试


by _____QWQ_____ @ 2023-11-08 21:59:59

#include <iostream>
#include <cstring>
using namespace std;
bool SelectYear(int x)
{
    if(x % 100 != 0 && x % 4 == 0 || x % 400 == 0){
        return true;
    }
    return false;
}
int main(){
    int n,m;
    cin>>n>>m;
    int a[3000];
    int sum=0;
    for(int i=n;i<=m;i++)
    {
        if(SelectYear(i))
        {
            sum++;
            a[sum]=i;
        }
    }
    cout<<sum<<endl;
    for(int i=1;i<=sum;i++)
    {
        cout<<a[i]<<" ";
    }
    return 0;
}

用你的函数ac100过,应该是主函数的问题吧


by suyulong1212 @ 2023-11-08 22:00:12

@__QAQ__ 那里显然要打括号吧。。。


by steven609 @ 2023-11-08 22:01:45

@elong123 试试把所有的main换成boot(你的程序没有大问题,观察细节,或许成功。)


by steven609 @ 2023-11-08 22:03:24

boot main()


by suyulong1212 @ 2023-11-08 22:04:11

@__QAQ__ 他用的c呀,c编译器没c++那么聪明


by elong123 @ 2023-11-08 22:20:48

@__QAQ__ 感谢我将主函数中的while循环改为for循环就ac了,虽然但是不知道这里错误的原因,大佬...


by elong123 @ 2023-11-08 22:23:14

bool SelectYear(int x){
    if(x % 100 != 0 && x % 4 == 0 || x % 400 == 0){
        return true;
    }
    return false;
}
int main(){
    int x,y;
    int sum = 0;
    int a[2000];
    scanf("%d %d",&x,&y);
//  bool s = SelectYear(x);
    for(int i = x; i <= y; i++){
        if(SelectYear(x)){
            a[sum] = x;
            sum++;
        }
        x++;        
    }
    printf("%d\n",sum);
    for(int j = 0; j < sum; j++){
        printf("%d ",a[j]);
    }
    return 0;
}

这样可以ac了


|