题目描述
从 \red{1~n}1~n 这 \red{n}n 个整数中随机选取任意多个,输出所有可能的选择方案。
输入格式
输入一个整数\red{n}n。
输出格式
每行输出一种方案。
同一行内的数必须升序排列,相邻两个数用恰好 \red{1}1个空格隔开。
本题有自定义校验器(SPJ
),各行(不同方案)之间的顺序任意
样例
输入数据
3
Copy
输出数据
3
2
2 3
1
1 3
1 2
1 2 3
代码如下:
#include <queue>
#include <math.h>
#include <stack>
#include <stdio.h>
#include <iostream>
#include <vector>
#include <iomanip>
#include <string.h>
#include <algorithm>
using namespace std;
#define LL long long
const int N = 1e5 + 10;
const int INF = 0x3f3f3f3f;
int a[1000];
void f(int n,int step,int last)
{
if(step > 0)
{
for(int i = 0 ; i < step ; i++)
cout << a[i] << " ";
cout << endl;
}
for(int i = last+1 ; i <= n ;i++)
{
a[step] = i;
f(n,step+1,i);
}
}
int main()
{
int n;
cin >> n;
f(n, 0,0);
return 0;
}