前面的点WA

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

hky123_ @ 2021-09-05 11:07:49

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int n,q,a[10000001],x,y;
int pd(int x,int y)
{
    int i,z=10,minn=0x7fffffff,f=0;
    for(i=2;i<=x;i++)
        z*=10;
    for(i=1;i<=n;i++)
        if(a[i]%z==y&&a[i]<minn)
        {
            f=1;
            minn=a[i];
        }
    if(f==0)
        return -1;
    return minn;
}
int main()
{
    int i;
    cin>>n>>q;
    for(i=1;i<=n;i++)
        cin>>a[i];
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        cout<<pd(x,y)<<endl;
    }
    return 0;
}

by hky123_ @ 2021-09-05 11:19:50

//新代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
int n,q,a[10000001],x,y;
int pd(int x,int y)
{
    int i,z=1,minn=0x7fffffff,f=0;
    for(i=1;i<=x;i++)
        z*=10;
    for(i=1;i<=n;i++)
        if(a[i]%z==y&&a[i]<minn)
        {
            f=1;
            minn=a[i];
        }
    if(f==0)
        return -1;
    return minn;
}
int main()
{
    int i;
    cin>>n>>q;
    for(i=1;i<=n;i++)
        cin>>a[i];
    for(i=1;i<=n;i++)
    {
        cin>>x>>y;
        cout<<pd(x,y)<<endl;
    }
    return 0;
}

|