Description
Now you are given one non-negative integer n in 10-base notation, it will only contain digits ('0'-'9'). You are allowed to choose 2 integers i and j, such that: i!=j, 1≤i<j≤|n|, here |n| means the length of n’s 10-base notation. Then we can swap n[i] and n[j].
For example, n=9012, we choose i=1, j=3, then we swap n[1] and n[3], then we get 1092, which is smaller than the original n.
Now you are allowed to operate at most M times, so what is the smallest number you can get after the operation(s)?
Please note that in this problem, leading zero is not allowed!
Input
The first line of the input contains an integer T (T≤100), indicating the number of test cases.
Then T cases, for any case, only 2 integers n and M (0≤n<10^1000, 0≤M≤100) in a single line.
Output
Sample Input
3 9012 0 9012 1 9012 2
Sample Output
9012 1092 1029
1 # include<stdio.h> 2 # include<string.h> 3 int main() 4 { 5 int T,n,i,j,k; 6 char s[10010]; 7 char temp; 8 int max,min; 9 bool flag; 10 scanf("%d",&T); 11 for(i=1;i<=T;i++) 12 { 13 scanf("%s%d",s,&n); 14 int len=strlen(s); 15 if(n==0) 16 { 17 printf("%s\n",s); 18 continue; 19 } 20 min=s[0]; 21 int minx=0; 22 for(j=len-1;j>0;j--) 23 { 24 if(s[j]<min && s[j] !='0'){ 25 min=s[j]; 26 minx=j; 27 28 } 29 } 30 if( s[0]>min){ 31 temp = s[0]; s[0]=s[minx]; s[minx]=temp; 32 n--; 33 } 34 while(n--) 35 { 36 flag=0; 37 for(j=1;j<len;j++){ 38 min=s[j]; 39 minx=j; 40 for(k=len-1;k>j;k--){ 41 if(s[k]<min) 42 { 43 min = s[k]; 44 minx=k; 45 flag=1; 46 } 47 } 48 if(flag) { temp= s[minx]; s[minx]=s[j]; s[j]=temp; break;} 49 } 50 } 51 printf("%s\n",s); 52 } 53 return 0; 54 }