代码如下
#include <iostream>
#include <stdio.h>
#include <algorithm>
using namespace std;
int main()
{
int i,n,a[20],k;
scanf("%d",&n);
while(n--)
{
scanf("%d",&i);
for(k=0;k<i-1;k++) {a[k]=k+1;printf("%d",k+1);}
printf("%d\n",k+1);a[k]=k+1;
while(next_permutation(a,a+i))
{
for(k=0;k<i-1;k++) printf("%d",a[k]);printf("%d\n",a[k]);
}
}
return 0;
}
next(prev)_permutation函数:让范围内数据按照字典序由大到小(由小到大)进行原址排序,若无更大(小)排列方式返回false(bool)