123asd45__ @ 2023-07-16 19:50:40
#include<iostream>
#include<algorithm>
using namespace std;
int a[100000];
int w[8]={1,10,100,1000,10000,100000,1000000,10000000};
int main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a+1,a+n+1);
int ans=0;
while(q--){
int x,y;
cin>>x>>y;
for(int i=1;i<=n;i++){
int cnt=a[i]%w[x];
if(cnt==y){
ans=a[i];
break;
}
}
cout<<ans<<endl;
}
return 0;
}
by PC114514 @ 2023-07-16 20:14:34
改完了,AC
1.长度小于10000000,你数组就写8个肯定不够 pow(a,b)就是a的b次方
2.ans更新放在循环里,否则如果无解输出的是上一个答案
Code:
#include<bits/stdc++.h>
using namespace std;
int a[1005];
int main(){
int n,q;
cin>>n>>q;
for(int i=1;i<=n;i++){
cin>>a[i];
}
sort(a,a+n);
while(q--){
int x,y;
cin>>x>>y;
int ans=-1;
for(int i=1;i<=n;i++){
int cnt=a[i]%int(pow(10,x));
if(cnt==y){
ans=a[i];
break;
}
}
cout<<ans<<endl;
}
return 0;
}
by PC114514 @ 2023-07-16 20:15:19
@123asd45__
by __lalala__ @ 2023-08-03 11:46:03
@ PC114514 第十行sort用错了 该这样
sort(a + 1,a + 1 + n);
by __lalala__ @ 2023-08-03 11:46:29
@ PC114514
by __lalala__ @ 2023-08-03 11:46:55
@PC114514
by PC114514 @ 2023-08-05 17:31:48
好的,感谢 @mei_you_ming_zi