组合输出题解
查看原帖
组合输出题解
1579911
limuxiluogu楼主2025/2/7 21:42
#include<bits/stdc++.h>
using namespace std;
int countbit(int n){
    int ans=0;
    while(n){
        ++ans;
        n-=(n & -n);
    }
	return ans;
}
int main(){
    int n,r;
    cin>>n>>r;
    for(int i=(1<<n)-1;i>=0;--i){
        if(countbit(i)==r){
            for(int j=1;j<=n;++j){
                if(i & (1<<(n-j))){
                    cout<<setw(3)<<j;
                }
            }
            cout<<"\n";
        }
    }
}
2025/2/7 21:42
加载中...