题目大意:给出一个数num,然后可以进行m次交换(即可以交换不同位数上的数),使得新的数越小。(0不能放在第一位)
解题思路:贪心,每次把最小的数字换到尽量前的位置,第一位特殊处理。
#include <stdio.h>
#include <string.h>
const int N = 1005;
int m;
char num[N];
void swap(char& a, char& b) {
char c = a;
a = b;
b = c;
}
void solve() {
int len = strlen(num);
if (m == 0) return;
int c = num[0] - '0', id = len;
for (int j = len - 1; j; j--)
if (num[j] - '0' < c && num[j] != '0') {
id = j, c = num[j] - '0';
}
if (c != num[0] - '0') {
swap(num[0], num[id]), m--;
}
for (int i = 1; i < len; i++) {
if (m == 0) return;
c = num[i] - '0';
for (int j = len - 1; j > i; j--) {
if (num[j] - '0' < c) {
id = j, c = num[j] - '0';
}
}
if (c != num[i] - '0') {
swap(num[i], num[id]), m--;
}
}
}
int main() {
int cas;
scanf("%d", &cas);
while (cas--) {
scanf("%s%d", num, &m);
solve();
printf("%s\n", num);
}
return 0;
}