:枚举排列)
审题本题需要我们找出所有排列方式并按照字典序排序输出思路方法一dfs深度优先搜索由于最后还需要我们按照字典序输出且无法事先确定需要的for循环层数所以我们这里不能采用简单的for循环解决决策树在选取的时候我们依次选取前面没有选过的数插入即可最后选取满k就输出答案解题#includeiostream #includevector using namespace std; int n, k; vectorint path; bool judge[10]; void dfs() { if (path.size() k) { for (auto e : path) cout e ; cout endl; return; } for (int i 1; i n; i) { //数据选取 if (judge[i] true) continue;//选择过该数跳过 path.push_back(i); judge[i] true; dfs(); //数据回退 path.pop_back(); judge[i] false; } } int main() { cin n k; dfs(); return 0; }注意1.使用bool数组来判断对应数据是否选取过为false就是没选过true就是选过2.在选择完成和回溯的时候要注意将数据选取状态调整回来B3623 枚举排列递归实现排列型枚举 - 洛谷