40分求助

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

longtong @ 2022-10-10 16:45:42

#include<bits/stdc++.h>
using namespace std;
int book[1005],bian[1005],read[1005],visit[1005];
int m,n,ans,flag,c;
int main()
{
    cin>>n>>m;
    for(int i=1;i<=n;i++)
    {
        cin>>book[i];
    }
    for(int i=1;i<=m;i++)
    {
        cin>>bian[i];
        cin>>read[i];
    }
    sort(book+1,book+n+1);
    for(int i=1;i<=m;i++)
    {
        for(int j=1;j<=n;j++)
        {
            if(book[j]-read[i]>=0&&(book[j]-read[i])%10==0&&bian[i]<=book[j]&&visit[j]==0)
            {
                cout<<book[j]<<endl;
                visit[j]=1;
                flag=1;
                break;
            }
        }
        if(flag==0)
        {
            cout<<"-1"<<endl;
        }
        flag=0;
    }
    return 0;

}

by zi_jin @ 2022-10-10 17:16:03

这是我的代码

#include<bits/stdc++.h>
using namespace std;
int len;
int reader;
int n,q;
int Mod[8]={0,10,100,1000,10000,100000,1000000,10000000};
int book[1010];
int answer(){
    for(int x=1;x<=n;x++){
        if(book[x]%Mod[len]==reader) return book[x];
    }
    return -1;
}
int main(){
    cin>>n>>q;
    for(int i=1;i<=n;i++){
        cin>>book[i];
    }
    sort(book+1,book+n+1);
    for(int i=1;i<=q;i++){
        cin>>len>>reader;
        cout<<answer()<<endl;
    }
}

by zi_jin @ 2022-10-10 17:20:23

判断那里有点问题


by zi_jin @ 2022-10-10 17:23:49

bian存的是需求码长度,不可以和book数组直接比较


by zi_jin @ 2022-10-10 17:29:42

还有如果book[i]=34567,read[j]=997时,也算是成功匹配了,建议你还是改成我那种方式,把取模的值做成数组存储起来


by longtong @ 2022-10-10 19:12:03

@zi_jin 谢谢!


|