第36553题 组合题
完善程序(第二题):全排列

下面的程序按字典序输出 1 到 n 的全部排列。程序使用深度优先搜索,逐位确定每个位置上的数字,并用 used 数组标记某个数字是否已被使用;当搜索到第 n+1 位时输出一个完整排列,随后回溯。试补全程序。

#include <cstdio>

int n;
int a[15];
bool used[15];

void dfs(int step) {
    if (step > n) {
        for (int i = 1; i <= n; ++i)
            printf("%d ", a[i]);
        printf("\n");
        return;
    }
    for (int i = 1; i <= n; ++i) {
        if (___①___) {
            ___②___
            a[step] = i;
            dfs(___③___);
            ___④___
        }
    }
}

int main() {
    scanf("%d", &n);
    dfs(___⑤___);
    return 0;
}
{{ option.label }}
子题{{ index + 1 }} {{ child.type_label }}
{{ option.label }}
✓ 正确 ◐ 部分正确 ✗ 错误
程序运行统计
暂无判题统计
知识点:
提交{{ questionInfo.stats ? questionInfo.stats.submit_count : 0 }}次 正确率{{ statsAccuracy }}%
答案解析