From 8a2828153b3804ab02b279fd83b86c0b2223ba1c Mon Sep 17 00:00:00 2001 From: hanyixuanten Date: Fri, 14 Aug 2026 10:17:33 +0800 Subject: [PATCH] new file: 8.cpp --- 8.cpp | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 8.cpp diff --git a/8.cpp b/8.cpp new file mode 100644 index 0000000..0a39b85 --- /dev/null +++ b/8.cpp @@ -0,0 +1,36 @@ +// 94 +#include + +using namespace std; + +const int N = 20; +int n; +bool st[N]; // 数字是否用过 +int path[N]; // 保存数列 + +void dfs(int cnt) +{ + if (cnt > n) + { + for (int i = 1; i <= n; i++) + printf("%d ", path[i]); + puts(""); + return; + } + for (int i = 1; i <= n; i++) + { + if (!st[i]) + { + path[cnt] = i, st[i] = 1; + dfs(cnt + 1); + path[cnt] = 0, st[i] = 0; + } + } +} + +int main() +{ + scanf("%d", &n); + dfs(1); + return 0; +} \ No newline at end of file