当前位置:网站首页>P1157 combined output (DFS)

P1157 combined output (DFS)

2021-08-10 08:04:37 wx6110fa547fd20

P1157 Combined output (DFS)

  Subject portal

The question : Given n , r Output in dictionary order C(n,r) Number of all combinations in

Ideas : use DFS From small to large search can obviously .

#include<bits/stdc++.h>
using namespace std;
int a[25],vis[25],n,r;
void dfs(int x,int cnt){//x Represents the number at the end of the current  cnt Indicates the number of in the current combination  
	if(cnt==r){// The end condition  
		for(int i=1;i<=r;i++)
			printf(i==r?"%3d\n":"%3d",a[i]);
		return;
	}
	for(int i=1;i<=n;i++){
		if(!vis[i]&&i>x)
		{
			vis[i]=1;
			a[cnt+1]=i;
			dfs(i,cnt+1);
			vis[i]=0;
		}
	}
}
int main(){
	cin>>n>>r;
	dfs(0,0);
	return 0;
}

      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

版权声明
本文为[wx6110fa547fd20]所创,转载请带上原文链接,感谢
https://chowdera.com/2021/08/20210810080205576m.html

随机推荐