您的位置:首页 > 其它

42.递归算法---数的划分

2016-03-04 21:09 246 查看
2001年NOIP全国联赛提高组

时间限制: 1
s

空间限制: 128000
KB

题目等级
: 黄金
Gold

题解

题目描述 Description

将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序)。

例如:n=7,k=3,下面三种划分方案被认为是相同的。

1 1 5

1 5 1

5 1 1
问有多少种不同的分法。

输入描述 Input
Description


输入:n,k (6,2<=k<=6)

输出描述 Output
Description


输出:一个整数,即不同的分法。

样例输入 Sample
Input


7 3

样例输出 Sample
Output


4

数据范围及提示 Data
Size & Hint


{四种分法为:1,1,5;1,2,4;1,3,3;2,2,3;}

代码:

#include

using namespace std;

#include

int f(int,int,int);

int main()

{

int
n,k;

cin>>n>>k;

cout<<f(n,k,1)<<endl;

return 0;

}

int f(int a,int b,int c)

{

int g=0;

if(b==1) return 1;

else{

for(int i=c;i<=a/b;++i)//i<=a/b的含义就是当前的a/b,a分为b分,每一份起码大于i曾能再用i去分a这个数,

g+=f(a-i,b-1,i);

return g;

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: