Sample Input
5
12 1
213 2
998244353 1
998244353 2
998244353 3
Sample Output
12 21
123 321
298944353 998544323
238944359 998544332
233944859 998544332
题意:给你数字n和k,可以交换任意两位上的数字,最多交换k次,输出最小值和最大值
思路:想了那么多,结果dfs跑一下就过了
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<iostream>
using namespace std;
typedef long long ll;
int a[20];
int b[20];
int cnt;
int n,k;
int mmin,mmax;
void dfs(int pos,int step){
if(pos>=cnt)return ;
if(step>k)return ;
if(a[0]==0)return ;
int sum=0;
for(int i=0;i<cnt;i++){
sum=sum*10+a[i];
}
mmin=min(mmin,sum);
mmax=max(mmax,sum);
for(int i=pos;i<cnt;i++){
swap(a[pos],a[i]);
if(i==pos)dfs(pos+1,step);
else dfs(pos+1,step+1);
swap(a[pos],a[i]);
}
return ;
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d%d",&n,&k);
cnt=0;
int x=n;
while(x){
b[cnt++]=x%10;
x/=10;
}
for(int i=0;i<cnt;i++){
a[i]=b[cnt-i-1];
}
mmin=1e9+1;//一开始敲了9个9,题目1e9是能取到的,瞎了
mmax=0;
dfs(0,0);
printf("%d %d\n",mmin,mmax);
}
return 0;
}