6 and 8_TLE_help
查看原帖
6 and 8_TLE_help
1116303
_Mar楼主2024/3/18 21:59
//组合的输出
#include<iostream>
#include<iomanip>
using namespace std;

int n,r;
bool book[30];
int a[30];

void dfs(int step){

    if(step==r+1){

        for(int i=2;i<=r;i++){
            if(a[i-1]>a[i])
                return;
        }
        
        for(int i=1;i<=r;i++){
            cout<<setw(3)<<a[i];
        }

        cout<<'\n';
        return ;
    }

    for(int i=1;i<=n;i++){

        if(!book[i]){
            a[step]=i;
            book[i]=true;
            dfs(step+1);
            book[i]=false;
        }
    }

}

int main(){
    ios_base::sync_with_stdio(0);
    cin.tie(0);cout.tie(0);

    cin>>n>>r;
    //从n个元素中抽r个元素
    dfs(1);
    return 0;
}
2024/3/18 21:59
加载中...