为什么这个程序只会输出-1???

P3955 [NOIP2017 普及组] 图书管理员

difficultlong @ 2024-09-28 10:03:39

#include<bits/stdc++.h>
using namespace std;
int n,q;
int tb[1001],fh[1001][9],xbc[1001],xb[1001];
int main(){
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&tb[i]);
    }
    for(int i=1;i<=q;i++){
        scanf("%d%d",&xbc[i],&xb[i]);

    }
    sort(tb+1,tb+n+1);
    for(int i=1;i<=n;i++){
        int a=tb[i];
        int j=1;
        while(a){
            fh[i][j]=a%10;
            a/=10;
            j++;
        }
    }
    bool flag;
    for(int i=1;i<=q;i++){
        int a=xb[i],m[9];
        int j=1;
        while(j<=xbc[i]){
            m[j]=a/10;
            a%=10;
            j++;
        }
        for(int k=1;k<=n;k++){
            int l=1;
            flag=true;
            while(l<=xbc[i]){
                if(m[l]!=fh[k][l]){
                    flag=false;
                    break;
                }
                l++;
            }
            if(flag){
                printf("%d",tb[i]);
                break;
            }
        }
        if(!flag){
            printf("-1");
        }
    }
    return 0;
}

by A18999620056A @ 2024-09-28 10:10:51

啊?为什么我用DEV编译后没问题啊?


by difficultlong @ 2024-09-28 11:10:07

@A18999620056A 没有编译问题,只是运行出来都是-1 (o(╥﹏╥)o)


by _czx6666_ @ 2024-09-28 13:03:13

bool flag; 摸初值 @difficultlong


by _czx6666_ @ 2024-09-28 13:05:23

bool flag;
    for(int i=1;i<=q;i++){
        int a=xb[i],m[9];
        int j=1;
        while(j<=xbc[i]){
            m[j]=a/10;
            a%=10;
            j++;
        }
        for(int k=1;k<=n;k++){
            int l=1;
            flag=true;
            while(l<=xbc[i]){
                if(m[l]!=fh[k][l]){
                    flag=false;
                    break;
                }
                l++;
            }
            if(flag){
                printf("%d",tb[i]);
                break;
            }
        }
        if(!flag){
            printf("-1");
        }
    }

改```

for(int i=1;i<=q;i++){
 bool flag=0;
    int a=xb[i],m[9];
    int j=1;
    while(j<=xbc[i]){
        m[j]=a/10;
        a%=10;
        j++;
    }
    for(int k=1;k<=n;k++){
        int l=1;
        flag=true;
        while(l<=xbc[i]){
            if(m[l]!=fh[k][l]){
                flag=false;
                break;
            }
            l++;
        }
        if(flag){
            printf("%d",tb[i]);
            break;
        }
        if(!flag){
        printf("-1");
    }
    }

}

by difficultlong @ 2024-09-28 18:17:58

@czx6666 好像不对啊!也是这个效果,都是-1


by luogu_hezhenmin1 @ 2024-09-28 18:37:06

#include<bits/stdc++.h>
using namespace std;
const int MAXN=1e3+5;
const int INF=0x3ffffff;
int num[MAXN];
int main(){
    int n,q,xq1,xq2,i,m;
    cin>>n>>q;
    for(i=1;i<=n;i++) cin>>num[i];
    int mod,minn;
    for(m=1;m<=q;m++){
        mod=1;minn=INF;
        cin>>xq1>>xq2;
        for(i=1;i<=xq1;i++) mod*=10;
        for(i=1;i<=n;i++) if(num[i]%mod==xq2 and num[i]<minn) minn=num[i];
        if(minn==INF) cout<<-1<<endl;
        else cout<<minn<<endl;
    }
    return 0;
} 

我的代码


by difficultlong @ 2024-09-28 19:05:10

@luogu_hezhenmin1 请问我这个代码是什么问题?(感谢大佬的帮助,但是我也想要我的代码出故障的地方)


by luogu_hezhenmin1 @ 2024-09-28 19:07:27

@difficultlong 能不能加上注释,有点看不懂


by difficultlong @ 2024-09-28 19:08:02

@luogu_hezhenmin1 好的


by luogu_hezhenmin1 @ 2024-09-28 19:14:40

@difficultlong

int l=1;
            flag=true;
            while(l<=xbc[i]){
                if(m[l]!=fh[k][l]){
                    flag=false;
                    break;
                }
                l++;
            }

这一段错了,改成:

        int l=1;
            flag=false;
            while(l<=xbc[i]){
                if(m[l]==fh[k][l]){
                    flag=true;
                    break;
                }
                l++;
            }

| 下一页