题目
![](https://i-blog.csdnimg.cn/blog_migrate/3c722226bd2da50267e556cdc13eedf1.png)
题目的关键:
![](https://i-blog.csdnimg.cn/blog_migrate/c7649325584934dabaa59cdcaf65eff3.png)
思路:递归调用
答案结构:
![](https://i-blog.csdnimg.cn/blog_migrate/a2f2fec2aee65d6a8d612163e133282b.png)
每层遍历:
![](https://i-blog.csdnimg.cn/blog_migrate/0b323d14c8e6ea1156410167f034473c.png)
代码
#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;
}