递归组合型枚举

这个就要求没有重复的,可以按照字典序来输出,保证一定后面的数字比前面的数字要大,自然就没有重复的数字了,这个时候连判重也不需要了

https://www.acwing.com/activity/content/record/19/1/

#include<iostream>
using namespace std;
int n,m;
int p[30];
void dfs(int u,int start)
{
    if(u>m)
    {
        for(int i=1;i<=m;i++)
            cout<<p[i]<<" ";
        cout<<endl;
        return ;
    }
    for(int i=start;i<=n;i++)
    {
        p[u]=i;
        dfs(u+1,i+1);
        p[u]=0;
    }
}
int main(){
    cin>>n>>m;
    dfs(1,1);
    return 0;
}

 

posted @ 2022-04-02 13:23  小志61314  阅读(28)  评论(0)    收藏  举报