题意:对于一个数可以交换第i位,第j位的数字,求经过这样的M次步骤,能得到的最小值
思路:显然每次都是将最小的值移到前面去,那么我们i记录已经确定了几个最小的值,每确定一次就将最小值移到当前的下标,否则m++,继续找,注意一些第一位是0,和最小值就是当前下标值的处理
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 1000;
int n,m;
int a[MAXN];
int main(){
int t;
scanf("%d",&t);
while (t--){
scanf("%d%d",&n,&m);
memset(a,0,sizeof(a));
int t = n,cnt = 0;
while (t){
a[cnt++] = t%10;
t /= 10;
}
for (int i = 0; i < cnt/2; i++)
swap(a[i],a[cnt-1-i]);
for (int i = 0; i < m; i++){
if (i >= cnt)
break;
if (a[i] != 0){
int Min = a[i];
int sign = i;
for (int j = i; j < cnt; j++){
if (a[j] == 0 && i == 0)
continue;
if (Min > a[j])
Min = a[j],sign = j;
}
if (Min == a[i])
m++;
else swap(a[i],a[sign]);
}
else m++;
}
for (int i = 0; i < cnt; i++)
printf("%d",a[i]);
printf("\n");
}
return 0;
}