2 solutions
-
0
简单题
#include<iostream> using namespace std; int n,a[10]; bool b[10]={0}; void dfs(int x){ if(x==n){ for(int i=0;i<n;i++){ cout<<" "<<a[i]; } cout<<endl; return; } for(int i=1;i<=n;i++){ if(!b[i]){ b[i]=true; a[x]=i; dfs(x+1); b[i]=false; a[x]=0; } } } int main(){ cin>>n; dfs(0); }
-
0
#include<bits/stdc++.h> using namespace std; int ans[10],n; bool vis[10]; void dfs(int deep) { if (deep == n+1) { for (int i = 1;i <= n;i++) { cout << " " << ans[i]; } cout << endl; return; } else { for (int i = 1;i <= n;i++) { if (vis[i] == true) continue; vis[i] = true; ans[deep] = i; dfs(deep + 1); vis[i] = false; } } } int main() { cin >> n; dfs(1); return 0; }
- 1
Information
- ID
- 596
- Time
- 1000ms
- Memory
- 256MiB
- Difficulty
- 5
- Tags
- # Submissions
- 25
- Accepted
- 13
- Uploaded By