当前位置:网站首页>P1025 division of number (DFS)

P1025 division of number (DFS)

2021-08-10 08:04:48 wx6110fa547fd20

P1025 The division of numbers (DFS)

  Subject portal

The question : Sum for k The sum of the numbers is n Total number of combinations .

Ideas : Obviously, the combination only needs to be in ascending order DFS that will do .

#include<cstdio>
int n,k;
int dfs(int cnt,int x,int re){//re(remain)
	if(cnt==k) return re==0;
	if(re==0) return cnt==k;
	int ans=0;
	for(int i=x;i<=re;i++)
		ans+=dfs(cnt+1,i,re-i);
	return ans;
}
int main(){
	scanf("%d%d",&n,&k);
	printf("%d\n",dfs(0,1,n));
	return 0;
} 

      
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.

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

随机推荐