【题目】

从 1~n 这 n 个整数中随机选出 m 个,输出所有可能的选择方案。, ,

按照从小到大的顺序输出所有方案,每行1个。
首先,同一行内的数升序排列,相邻两个数用一个空格隔开。其次,对于两个不同的行,对应下标的数一一比较,字典序较小的排在前面(例如1 3 9 12排在1 3 10 11前面)。

【题解】

即从个数中选个出来,并且有字典序输出要求。这样的话,我们就需要把选择数的方法:分为选和不选。选的话用数组记录,而要字典序输出,那么遍历的优先就是小的数,让程序尽可能选择小的数。这样的话,我们就可以让其从最小的数1开始向大的数递增遍历,然后让其在遍历的过程中,尽可能的选,直到满足要求选择的数量,把数组中的值输出。

时间复杂度:

#include<iostream>
#include<cstring>
#include<sstream>
#include<string>
#include<cstdio>
#include<cctype>
#include<vector>
#include<queue>
#include<cmath>
#include<stack>
#include<list>
#include<set>
#include<map>
#include<algorithm>
#define fi first
#define se second
#define MP make_pair
#define P pair<int,int>
#define PLL pair<ll,ll>
#define lc (p<<1)
#define rc (p<<1|1)  
#define MID (tree[p].l+tree[p].r)>>1
#define Sca(x) scanf("%d",&x)
#define Sca2(x,y) scanf("%d%d",&x,&y)
#define Sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define Scl(x) scanf("%lld",&x)
#define Scl2(x,y) scanf("%lld%lld",&x,&y)
#define Scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define Pri(x) printf("%d\n",x)
#define Prl(x) printf("%lld\n",x)
#define For(i,x,y) for(int i=x;i<=y;i++)
#define _For(i,x,y) for(int i=x;i>=y;i--)
#define FAST_IO std::ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
#define STOP system("pause")
#define ll long long
const int INF=0x3f3f3f3f;
const ll INFL=0x3f3f3f3f3f3f3f3f;
const double Pi = acos(-1.0);
using namespace std;
template <class T>void tomax(T&a,T b){ a=max(a,b); } 
template <class T>void tomin(T&a,T b){ a=min(a,b); }
const int N=50+5;
int out[N],n,m;
void dfs(int num,int k){
    if(k==m+1){
        for(int i=1;i<k;i++)
            printf("%d ",out[i]);
        puts("");
        return ; 
    }
    if(num>n) return ;
    out[k]=num;
    dfs(num+1,k+1);
    dfs(num+1,k);
}
int main(){
    Sca2(n,m);
    dfs(1,1);

}