标签:
把自然数N分解为若干个自然数之和,输出方案数。
N,(1≤n≤50)
方案数
5
7
5 可分为
1 1 1 1 1
1 1 1 2
1 1 3
1 2 2
1 4
2 3
5
#include<cstdio>
using namespace std;
int a[100]={1},n,s;
void cf(int x,int y)
{
int i;
for(i=a[y-1];i<=x;i++)
if(i<=n)
{
a[y]=i;
x-=i;//减去数i,再继续拆分x
if(x==0)//拆分结束
s++;//累加方案数
else
cf(x,y+1);
x+=i;//回溯,以便可以产生所有可能的情况
}
}
int main()
{
scanf("%d",&n);
cf(n,1);
printf("%d",s);
return 0;
}
类题练习:洛谷 p2404
标签:
原文地址:http://www.cnblogs.com/jyhywh/p/5573675.html