33分求助

B3623 枚举排列(递归实现排列型枚举)

oyyhcs @ 2023-08-12 11:41:52

#include<iostream>
using namespace std;
int n,m;
void dfs(int u,int sum,int state)//sum的意思是输出了几个数 
{
    if(sum+n-u<m)return;
    if(sum==m)
    {
        for(int i=0;i<n;i++)
        {
            if(state>>i&1)cout<<i+1<<" ";
        }
        cout<<endl;
        return ;
    }

    dfs(u+1,sum+1,state|1<<u);
    dfs(u+1,sum,state);

}

int main()
{

    cin>>n>>m;
    dfs(0,0,0);

    return 0;
}

by _AyachiNene @ 2023-08-12 11:59:46

@oyyhcs 要输出重复的,比如1 3和3 1


by oyyhcs @ 2023-08-12 14:21:55

@Darling_zero_two 哦哦原来是这样啊,谢谢你


|