题目链接
Problem Description
键盘输入一个高精度的正整数n(≤100位),去掉其中任意s个数字后剩下的数字按照原来的左右次序组成一个新的正整数。编程对给定的n与s,寻找一种方案,使得剩下的数字组成的新数最小。
Input
输入有多组 每组包括原始数n,要去掉的数字数s;
Output
输出去掉s个数后最小的数
Example Input
178543 4
Example Output
13
#include<bits/stdc++.h>
using namespace std;
int main(){
ios :: sync_with_stdio(false);
cin.tie(0);
string s;
int n,cnt,k;
while(cin >> s >> n){
if(n >= s.size()){
cout << 0 << endl;
continue;
}
cnt = 0;
for(int i = 0;i + 1 < s.size();i++){
if(s[i] > s[i + 1]){
s.erase(i,1);
if(++cnt == n){
break;
}
i -= 2;
}
}
if(cnt < n){
s.erase(s.size() - n + cnt,n - cnt);
}
for(k = 0;k < s.size();k++){
if(s[k] != '0'){
break;
}
}
if(k == s.size()){//全部为0
cout << 0 << endl;
}
else{ // 注意前导0!!!
cout << s.substr(k) << endl;
}
}
return 0;
}