#include <iostream>
#include <vector>
using namespace std;
template <typename T>
void generate_power_set(const vector<T>& S) {
for (int i = 1; i < (1 << S.size()); i++) {
int x = i;
for (int j = 0; x >> j; j++) {
if ((x >> j) & 1) {
cout << S[j];
if (x >> (j + 1)) {
cout << ',';
}
}
}
cout << endl;
}
}
int main(int argc, char *argv[]) {
srand(time(nullptr));
vector<char> S;
if (argc >= 2) {
for (int i = 1; i < argc; i++) {
S.emplace_back(argv[i][0]);
}
} else {
S.resize(rand() % 10 + 1);
for (int i = 0; i < S.size(); i++) {
S[i] = 'A' + i;
}
}
generate_power_set(S);
return 0;
}EPI 5.5 THE POWER SET
最新推荐文章于 2024-05-02 10:44:35 发布
本文介绍了一种使用C++编程语言生成给定集合所有可能子集(幂集)的方法。通过模板函数实现,适用于不同类型的数据集合。该程序支持从命令行参数中读取输入或自动生成字符集合。
355

被折叠的 条评论
为什么被折叠?



