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


 

2001NOIP全国联赛提高组

 时间限制: 1 s

 空间限制: 128000 KB

 题目等级 : 黄金 Gold

题解

题目描述 Description

将整数n分成k份,且每份不能为空,任意两种划分方案不能相同(不考虑顺序)
例如:n=7k=3,下面三种划分方案被认为是相同的。
1 1 5

1 5 1

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

输入描述 Input Description

输入:nk (62<=k<=6)

输出描述 Output Description


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

样例输入 Sample Input

 7 3

样例输出 Sample Output

4

数据范围及提示 Data Size & Hint

 {四种分法为:115;124;133;223;}

代码:

#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/ba分为b分,每一份起码大于i曾能再用i去分a这个数,

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

              return g;

       }

}

原文地址:https://www.cnblogs.com/csgc0131123/p/5290426.html