1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | #include <algorithm> #include <cstdio> #include <string> bool flag[27]; int n; int p[27]; int ans = 0; void dfs(int k) { if (k == n + 1) { ++ans; return; } for (int i = 1; i <= n; ++i) { if (flag[i]) continue; if (k > 1 && i == p[k - 1] + 1) continue; p[k] = i; flag[i] = true; dfs(k + 1); flag[i] = false; } return; } int main() { scanf("%d", &n); dfs(1); printf("%d\n", ans); return 0; } |
0 of 6 Questions completed
Questions:
You have already completed the quiz before. Hence you can not start it again.
Quiz is loading…
You must sign in or sign up to start the quiz.
You must first complete the following:
0 of 6 Questions answered correctly
Your time:
Time has elapsed
You have reached 0 of 0 point(s), (0)
Earned Point(s): 0 of 0, (0)
0 Essay(s) Pending (Possible Point(s): 0)
1、当输入的 n=3 的时候,程序输出的答案为 3。
2、在 dfs 函数运行过程中,k 的取值会满足 1≤k≤n+1。
2、删除第 19 行的 “flag[i] = false”,对答案不会产生影响。
4、当输入的 n=4 的时候,程序输出的答案为( )。
5、如果因为某些问题,导致程序运行第 25 行的 dfs 函数之前,数组 p 的初值并不全为 0,则对程序的影响是( )。
6、假如删去第 14 行的 if(flag[i])continue;,输入 3,得到的输出答案是( )。
