首先,如果我的思路可行,此题应评橙。其次,我样例过了,但是“稻花香里说丰年,听取WA声一片。”
代码如下:
#include<bits/stdc++.h>
using namespace std;
int t;
int up(int x,int y){
int t=x/y*y;
if(t==x) return x;
return t+y;
}
int down(int x,int y){
return x/y*y;
}
int len(int x){
int s=0;
while(x){x/=10;s++;}
return s;
}
pair<int,int> p(int l,int r){
int s=0,x=1;
while(up(l,x)<=down(r,x)) s++,x*=10;
x/=10;
s--;
if(up(l,x*5)<=down(r,x*5)) return make_pair(5*x,(len(l)-s)*2-1);
return make_pair(x,(len(l)-s)*2);
}
int f(int l,int r){
if(len(l)==len(r)) return p(l,r).first;
int mins=1e9,ans;
for(int i=len(l);i<=len(r);i++){
pair<int,int> t;
if(i==len(l)) t=p(l,pow(10,i)-1);
else if(i==len(r)) t=p(pow(10,i-1),r);
else t=p(pow(10,i-1),pow(10,i)-1);
if(t.second<mins){
mins=t.second;
ans=t.first;
}
}
return ans;
}
int main(){
scanf("%d",&t);
while(t--){
int l,r;
scanf("%d%d",&l,&r);
printf("%d\n",f(l,r));
}
return 0;
}