Files
python/GESP/Mkx/20250401T3.cpp
HuangHai 1f397eca87 'commit'
2025-08-30 18:35:01 +08:00

34 lines
999 B
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include <bits/stdc++.h>
using namespace std;
const int N = 110; // 定义最大数组大小
vector<int> vec; // 用于存储当前生成的子集
int n; // 输入的数字n表示从1到n生成子集
// 深度优先搜索函数,用于生成所有可能的子集
// u: 当前处理的数字
void dfs(int u) {
// 打印当前生成的子集
for (int i = 0; i < vec.size(); i++)
cout << vec[i] << " ";
cout << endl;
// 如果已经处理完所有数字,返回
if (u == n + 1)
return;
// 尝试将每个数字添加到当前子集中
for (int i = 1; i <= n; i++) {
// 确保子集中的数字按升序排列,避免重复
if (vec.size() && i <= vec.back())
continue;
vec.push_back(i); // 将数字i添加到子集中
dfs(u + 1); // 递归处理下一个数字
vec.pop_back(); // 回溯,移除最后添加的数字
}
}
int main() {
cin >> n; // 输入数字n
dfs(1); // 从数字1开始生成子集
return 0;
}