Kiki likes traveling. One day she finds a magic lamp, unfortunately the genie in the lamp is not so kind. Kiki must answer a question, and then the genie will realize one of her dreams.
The question is: give you an integer, you are allowed to delete exactly m digits. The left digits will form a new integer. You should make it minimum.
You are not allowed to change the order of the digits. Now can you help Kiki to realize her dream?
题意:
多组输入,每次给出一个不超过1000位的整数n,和一个m,
你要做的是在这个n中,去掉m位,使得这个整数尽可能的小(不能更改顺序)。
最后的输出要忽略前导零,
比如 n=1234 ,m=1, 那就输出 123
代码:
ll t,n,m;
string s;
ll ans[maxn];
int main(){
while(cin>>s>>n){
ll l=0,r=n,cnt=0;
n=s.size()-n;
while(n--){
int pos=l;
for(int i=l;i<=r;i++){
if(s[i] < s[pos]) pos=i;
}
ans[cnt++] = s[pos]-'0';
l=pos+1;
r++;
}
int pos=0;
while(ans[pos]==0 && pos<cnt) pos++;
if(pos==cnt) cout<<0;
for(;pos<cnt;pos++){
cout<<ans[pos];
}puts("");
}
return 0;
}
实际写代码的时候,s就是n, n就是m
这里先让 l=0,r=n;让左坐标和右坐标卡范围,当然,右坐标要取从n到size()-1慢慢递增,因为有留出足够的位数,不然能读满m位。
然后 让 n=s.size()-n;
这就代表要剩下几个。
每次在范围里找最小的数字,然后存到a数组里去。
最后还要处理前导零。