qiufangguo @ 2024-07-25 09:43:58
#include<bits/stdc++.h>
using namespace std;
int n,q,a[1001],c;
long long int d=1;
struct reader{
long long int x;
int y;//x:长度 y:需求码
}b[1001];
int main(){
//freopen("librarian.in","r",stdin);
//freopen("librarian.out","w",stdout);
scanf("%d%d",&n,&q);
for(int i=0;i<n;i++){
scanf("%d",&a[i]);
}
for(int i=0;i<q;i++){
scanf("%d%d",&c,&b[i].y);
d=1;
for(int i=0;i<c;i++){
d*=10;
}
b[i].x=d;
}
for(int i=0;i<q;i++){
int minn=-1;
for(int j=0;j<n;j++){
if(a[j]%b[i].x==b[i].y){
if(minn=-1||a[j]<minn)minn=a[j];
}
}
printf("%d\n",minn);
}
}
by chris2012yb @ 2024-07-25 14:06:46
不用那么复杂:```
using namespace std; typedef long long ll; ll n,k,a[1010],ans[1010]; struct read{ ll l,num; }b[1010]; int main(){ cin>>n>>k; for(int i=1;i<=k+2;i++) ans[i]=1e9; for(int i=1;i<=n;i++) cin>>a[i]; for(int i=1;i<=k;i++) cin>>b[i].l>>b[i].num; for(int i=1;i<=n;i++) for(int j=1;j<=k;j++){ ll q=pow(10,b[j].l); if(a[i]%q==b[j].num) ans[j]=min(ans[j],a[i]); } for(int i=1;i<=k;i++) if(ans[i]==1e9) cout<<"-1"<<endl; else cout<<ans[i]<<endl; return 0; }
by chris2012yb @ 2024-07-25 14:07:43
不用那么复杂:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll n,k,a[1010],ans[1010];
struct read{
ll l,num;
}b[1010];
int main(){
cin>>n>>k;
for(int i=1;i<=k+2;i++)
ans[i]=1e9;
for(int i=1;i<=n;i++)
cin>>a[i];
for(int i=1;i<=k;i++)
cin>>b[i].l>>b[i].num;
for(int i=1;i<=n;i++)
for(int j=1;j<=k;j++){
ll q=pow(10,b[j].l);
if(a[i]%q==b[j].num)
ans[j]=min(ans[j],a[i]);
}
for(int i=1;i<=k;i++)
if(ans[i]==1e9)
cout<<"-1"<<endl;
else
cout<<ans[i]<<endl;
return 0;
}
by chris2012yb @ 2024-07-25 14:09:03
@qiufangguo