挑战题之排列生成
挑战题之排列生成
Time Limit: 2000MS | Memory Limit: 65535KB |
Submissions: 435 | Accepted: 170 |
Sample Input
3
Sample Output
123 132 213 231 312 321
#include<iostream> #include <cstdio> using namespace std; int s[10]; int s1[10]; int vis[10] = {0}; void dfs(int l,int n ) { int i; if(l>=n) { for(i= 0;i<n;i++) printf("%d",s1[i]); printf("\n"); } for(i=0;i<n;i++) { if(!vis[i]) { vis[i] = 1; s1[l] = s[i]; dfs(l+1,n); vis[i] = 0; } } } int main() { int num; cin>>num; for(int i=0;i<num;i++) s[i] = i+1; dfs (0, num); return 0; }