3 条题解
-
1
#include <iostream> #include <algorithm> #include <vector> using namespace std; int main() { int n = 0; cin >> n; vector<int> a; for(int i = 0; i < n; ++ i) a.push_back(i + 1); do { for(int i = 0; i < n; ++ i) cout << a[i] << ' '; cout << endl; }while(next_permutation(a.begin(), a.end())); return 0; }
-
1
#include<iostream> using namespace std; const int N = 10; int st[N], path[N];//st表示每个数字状态 int n; void dfs(int u) { if (u > n) { for (int i = 1; i <= n; ++ i) cout << path[i] << ' '; cout << endl; } for (int i = 1; i <= n; ++ i) { if (!st[i]) { path[u] = i; st[i] = 1; dfs(u + 1); st[i] = 0; } } } int main() { cin >> n; dfs(1); return 0; }
-
1
参考答案:
#include <iostream> using namespace std; const int N = 10; int n; int path[N]; void dfs(int u, int state) { if (u == n) { for (int i = 0; i < n; i ++ ) printf("%d ", path[i]); puts(""); return; } for (int i = 0; i < n; i ++ ) if (!(state >> i & 1)) { path[u] = i + 1; dfs(u + 1, state + (1 << i)); } } int main() { scanf("%d", &n); dfs(0, 0); return 0; }
- 1
信息
- ID
- 88
- 时间
- 1000ms
- 内存
- 256MiB
- 难度
- 10
- 标签
- 递交数
- 2
- 已通过
- 2
- 上传者