标签:
2 2 3
12 21 123 132 213 231 312 321
#include <iostream> #include <cstdio> #include <algorithm> using namespace std; int main(){ int T; int n; int a[10]={1,2,3,4,5,6,7,8,9}; scanf("%d",&T); while(T--){ scanf("%d",&n); do{ for(int i=0;i<n;i++){ printf("%d",a[i]); } printf("\n"); }while(next_permutation(a,a+n)); } return 0; }
回溯递归:
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int T; int n; int a[10]; int b[10];//是否已用 void fun(int x){ if(x==n){ for(int i=0;i<n;i++){ printf("%d",a[i]); } printf("\n"); }else{ for(int i=1;i<=n;i++){ if(b[i]==0){ a[x]=i; b[i]=1; fun(x+1); b[i]=0; } } } } int main(){ scanf("%d",&T); while(T--){ memset(b,0,sizeof(b)); scanf("%d",&n); fun(0); } return 0; }
标签:
原文地址:http://www.cnblogs.com/TWS-YIFEI/p/5872969.html