升阳 @ 2017-11-12 13:02:57
改之前的代码
#include <iostream>
using namespace std;
int bk[1001],nd[1001],ndl[1001];
int n,q;
int power(int a,int b){
int off=1;
while(b>1){
if(b&1){
off*=a;
b-=1;
}
a*=a;
b/=2;
}
return off*a;
}
int main(){
int i,j,mins;
cin>>n>>q;
for(i=1;i<=n;i++)
cin>>bk[i];
for(i=1;i<=q;i++)
cin>>ndl[i]>>nd[i];
for(i=1;i<=q;i++){
mins=0xfffffff;
for(j=1;j<=n;j++){
if(bk[j]<mins&&bk[j]%power(10,ndl[i])==nd[i])
mins=bk[j];
}
if(mins==0xffffffff)
cout<<-1<<endl;
else
cout<<mins<<endl;
}
return 0;
}
改之后的代码
#include <iostream>
using namespace std;
int bk[1001],nd[1001],ndl[1001];
int n,q;
int power(int a,int b){
int off=1;
while(b>1){
if(b&1){
off*=a;
b-=1;
}
a*=a;
b/=2;
}
return off*a;
}
int main(){
int i,j,mins;
cin>>n>>q;
for(i=1;i<=n;i++)
cin>>bk[i];
for(i=1;i<=q;i++)
cin>>ndl[i]>>nd[i];
for(i=1;i<=q;i++){
mins=0xfffffff;
for(j=1;j<=n;j++){
if(bk[j]<mins&&bk[j]%power(10,ndl[i])==nd[i])
mins=bk[j];
}
if(mins==0xfffffff)
cout<<-1<<endl;
else
cout<<mins<<endl;
}
return 0;
}
只是一个F,AK……
by __世界第一弱__ @ 2017-11-12 13:19:21
@jeffice0325 sort
by longlongzhu123 @ 2017-11-12 13:46:39
好诡异的迷之缩进
by 升阳 @ 2017-11-12 15:39:05
@世界第一弱 不用sort不超时
by themaster @ 2017-11-12 17:04:12
#include<stdio.h>
#include<algorithm>
#include<iostream>
using namespace std;
int a[1005];
int main()
{
int n,q,b,c,i,j,s=1,ss=0;
freopen("librarian.in","r",stdin);
freopen("librarian.out","w",stdout);
scanf("%d%d",&n,&q);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
for(i=0;i<q;i++)
{
scanf("%d %d",&b,&c);
s=1;ss=0;
for(j=1;j<=b;j++)
s=s*10;
for(j=0;j<n;j++)
{
if(a[j]%s==c)
{
printf("%d\n",a[j]);
ss=1;
break;
}
}
if(ss==0)printf("-1\n");
}
fclose(stdin);
fclose(stdout);
return 0;
}
by __世界第一弱__ @ 2017-11-12 20:10:39
@jeffice0325 用sort也不超时啊