题目
92. 递归实现指数型枚举 - AcWing题库
题目的关键:
思路:递归调用
答案结构:
每层遍历:
代码
#include<bits/stdc++.h>
using namespace std;
int n;
//构建答案类,方便记录和输出
class ans {
public:
int s[16];
int length;
//输出
void out() {
if (this->length == 0)return;
for (int i = 0; i < length; ++i) {
cout << this->s[i] << ' ';
}
cout << endl;
}
//增添
void operator+=(int b) {
++this->length;
this->s[length - 1] = b;
}
//删减
void operator-=(int b) {
--this->length;
}
};
class ans m = {{}, 0};
//bottom表示最多能从n遍历到bottom
void choose(int bottom) {
m.out();
for (int i = n; i >= bottom; --i) {
m += i;
choose(i + 1);
m -= i;//归位
}
}
int main() {
cin >> n;
choose(1);
return 0;
}