top1编程
← 返回题目
题解

【递归入门】组合的输出

1 条题解

  • 0
    @ 2026-7-29 0:16:31
    #include<bits/stdc++.h>
    using namespace std;
    long long n,a[99],b[99],r;
    void dfs(long long x){
        if(x>r){
            for(int i=1;i<=r;i++){
                cout<<a[i]<<" ";
            }
            cout<<"&#92;n";
        }else{
            for(int i=a[x-1]+1;i<=n;i++){
                if(!b[i]){
                    b[i]=1;
                    a[x]=i;
                    dfs(x+1);
                    b[i]=0;
                }
            }
        }
    }
    int main(){
        ios::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);
        cin>>n>>r;
        dfs(1);
        return 0;
    }
    
    • 1