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;
}
}
时间限制: 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;
}
}
相关文章推荐
- 42.递归算法---数的划分
- 42.递归算法---数的划分
- 排序算法-堆排序
- php中使用session避免在刷新页面造成反复提交表单
- traincascade相关结构体
- hdu 4288 离线线段树+间隔求和
- mybatis中association的column传入多个参数值
- hdu 4288 离线线段树+间隔求和
- HttpClient
- StrongReference weakedReference ,softReference的学习理解
- JS学习笔记-OO疑问之封装
- 有关Tomcat启动时出现端口占用的情况说明
- ab压力测试工具详解
- 构造器、方法重载、继承、方法重写、自动转型、多态课堂笔记
- 51nod p1175 区间中第K大的数
- C++之多继承
- C++之多继承
- C++之多继承
- Struts基础(Package配置详解)
- VS2010 + QT中调用CUDA