求助 60分 一种别样的方法
查看原帖
求助 60分 一种别样的方法
681514
Arthi楼主2022/8/12 15:29
#include <bits/stdc++.h>
using namespace std;

int n,r,comb[30]={},cnt=0,s=1;
int c(int m,int n){
	int s=1,s1=1,s2=1,s3=1;
	for(int i=1; i<=m; i++)s1*=i;
	for(int i=1; i<=n; i++)s2*=i;
	for(int i=1; i<=m-n; i++)s3*=i;
	s=s1/(s2*s3);
	return s;
}
void dis(){
	for(int i=1; i<=r; i++){
		cout<<setw(3)<<comb[i];
	}
	cnt++;
	cout<<endl;
}
void update(int d){
	if(cnt==s)return ;
	comb[d]++;
	for(int i=d+1; i<=r; i++){
		comb[i]=comb[i-1]+1;	
	}
	dis();
	if(comb[d]==n-r+d)update(d-1);
	if(d==r)update(d);
	if(d==1)update(r);
}
int main(){
	cin>>n>>r;
	s=c(n,r);
	for(int i=1; i<=r; i++){
		comb[i]=comb[i-1]+1;	
	}
	dis();
	update(r);	
	return 0;
}
2022/8/12 15:29
加载中...