View Code
1 #include <iostream> 2 #include <algorithm> 3 #include <cstring> 4 using namespace std; 5 6 int main() 7 { 8 int i,j,k,T; 9 int n; 10 char str[10]={'1','2','3','4','5','6','7','8','9'};//实际上可以吧10省略 11 cin>>T; 12 while(T--) 13 { 14 cin>>n; 15 k=0; 16 do 17 { 18 for(i=0;i<n;++i) 19 cout<<str[i]; 20 cout<<endl; 21 }while(next_permutation(str,str+n)); 22 } 23 return 0; 24 }
1 #include <stdio.h> 2 #include <string.h> 3 #define MAX 10 4 bool vis[MAX]; 5 int res[MAX]; 6 int N; 7 void output() 8 { 9 int i; 10 for(i = 0; i < N; i++) 11 printf("%d", res[i]); 12 printf("\n"); 13 } 14 void dfs(int step) 15 { 16 int i; 17 if(step == N) 18 output(); 19 else 20 { 21 for(i = 0; i < N; i++) 22 { 23 if(!vis[i]) 24 { 25 vis[i] = 1; 26 res[step] = i + 1; 27 dfs(step + 1); 28 vis[i] = 0; 29 } 30 } 31 } 32 } 33 34 int main() 35 { 36 int t; 37 scanf("%d",&t); 38 while(t--) 39 { 40 memset(vis,0,sizeof(vis)); 41 scanf("%d", &N); 42 dfs(0); 43 } 44 return 0; 45 } 46 47