以下引用部分全都来自:http://blog.csdn.net/ice_crazy/article/details/7478802  Ice—Crazy的专栏

分析:

HDU 1028
摘:
本题的意思是:整数划分问题是将一个正整数n拆成一组数连加并等于n的形式,且这组数中的最大加数不大于n。
如6的整数划分为

6
5 + 1
4 + 2, 4 + 1 + 1
3 + 3, 3 + 2 + 1, 3 + 1 + 1 + 1
2 + 2 + 2, 2 + 2 + 1 + 1, 2 + 1 + 1 + 1 + 1
1 + 1 + 1 + 1 + 1 + 1

共11种。下面介绍一种通过递归方法得到一个正整数的划分数。

递归函数的声明为 int split(int n, int m);其中n为要划分的正整数,m是划分中的最大加数(当m > n时,最大加数为n),
1 当n = 1或m = 1时,split的值为1,可根据上例看出,只有一个划分1 或 1 + 1 + 1 + 1 + 1 + 1
可用程序表示为if(n == 1 || m == 1) return 1;

2 下面看一看m 和 n的关系。它们有三种关系
(1) m > n
在整数划分中实际上最大加数不能大于n,因此在这种情况可以等价为split(n, n);
可用程序表示为if(m > n) return split(n, n);
(2) m = n
这种情况可用递归表示为split(n, m - 1) + 1,从以上例子中可以看出,就是最大加
数为6和小于6的划分之和
用程序表示为if(m == n) return (split(n, m - 1) + 1);
(3) m < n
这是最一般的情况,在划分的大多数时都是这种情况。
从上例可以看出,设m = 4,那split(6, 4)的值是最大加数小于4划分数和整数2的划分数的和。
因此,split(n, m)可表示为split(n, m - 1) + split(n - m, m)

递归代码如下:

#include<stdio.h>
#include<string.h> int f(int n,int m)
{
if(n==||m==)
return ;
if(n==m)
return f(n,m-)+;
if(m>n)
return f(n,n);
return f(n,m-)+f(n-m,m);
} int main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
printf("%d\n",f(n,n));
}
return ;
}

但本题不能直接用递归函数求解,会因为n太大而超时或因递归深度超过允许值发生错误,因此要加上dp的思想.

//我交了一次,超时了org

//所以可行的dp代码如下:

#include<stdio.h>
#include<string.h> int f(int n,int m)
{
if(n==||m==)
return ;
if(n==m)
return f(n,m-)+;
if(m>n)
return f(n,n);
return f(n,m-)+f(n-m,m);
} int main()
{
int n,i,j,f[][];
f[][]=;
//dp改编自递归
for(i=;i<=;i++)//i是要划分的正整数
{
for(j=;j<=;j++)//j是划分中的最大加数
{
if(i==||j==)
f[i][j]=;
else if(i==j)
f[i][j]=f[i][j-]+;
else if(j>i)
f[i][j]=f[i][i];
else if(i>j)
f[i][j]=f[i][j-]+f[i-j][j];
}
} while(scanf("%d",&n)!=EOF)
{
printf("%d\n",f[n][n]);
}
return ;
}

HDU 1028 Ignatius and the Princess III (递归,dp)的更多相关文章

  1. hdu 1028 Ignatius and the Princess III 简单dp

    题目链接:hdu 1028 Ignatius and the Princess III 题意:对于给定的n,问有多少种组成方式 思路:dp[i][j],i表示要求的数,j表示组成i的最大值,最后答案是 ...

  2. HDU 1028 Ignatius and the Princess III:dp or 母函数

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1028 题意: 给你一个正整数n,将n拆分成若干个正整数之和,问你有多少种方案. 注:"4 = ...

  3. HDU 1028 Ignatius and the Princess III 整数的划分问题(打表或者记忆化搜索)

    传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1028 Ignatius and the Princess III Time Limit: 2000/1 ...

  4. hdu 1028 Ignatius and the Princess III(DP)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  5. HDU 1028 Ignatius and the Princess III (母函数或者dp,找规律,)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  6. hdu 1028 Ignatius and the Princess III 母函数

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  7. hdu 1028 Ignatius and the Princess III (n的划分)

    Ignatius and the Princess III Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K ...

  8. HDU 1028 Ignatius and the Princess III (动态规划)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

  9. HDU 1028 Ignatius and the Princess III (生成函数/母函数)

    题目链接:HDU 1028 Problem Description "Well, it seems the first problem is too easy. I will let you ...

随机推荐

  1. apache 403错

    <Directory />Options FollowSymLinksAllowOverride NoneOrder deny,allowAllow from all</Direct ...

  2. Java学习中,常用的命令管理(Java 学习中的小记录)

    Java学习中,常用的命令管理      作者:王可利(Star·星星) 一.Window中常见的dos命令 在哪里操作dos命令: Win7 ---> 开始  ---->所有程序---& ...

  3. CodeForces 569A 第八次比赛 C题

    Description Little Lesha loves listening to music via his smartphone. But the smartphone doesn't hav ...

  4. ECMAScript5

    张鑫旭:ECMAScript5介绍 淘宝整理的es5-safe /********* It provides the following methods: Function.prototype.bin ...

  5. notepad++ erlang开发环境设置

    初学erlang 网上有使用eclipse的,有使用emacs的,尝试了一下, 感觉太麻烦,来试试notepad++吧. 有什么新使用方法会再更新上来,for you for me. 1.语法高亮: ...

  6. ExtJS MVC学习手记

    开始学习ExtJS的MVC了.这篇文章仅是用来做一个目录,为自己这个阶段的学习内容做个索引. 手记涉及的文章: EXTJS MVC结构(译自ExtJS4.0文档中的<MVC Architectu ...

  7. C#全局作用符::

    比如说你在全局定义了一个变量str,然后在函数里面又定义了这个str名字的变量的,这个时候你要是在函数里面直接写str,那么就是访问的函数内部的变量的.无法访问外部变量的.这是正常的现象的.但是如果你 ...

  8. HashSet<T>类

    HashSet<T>类主要是设计用来做高性能集运算的,例如对两个集合求交集.并集.差集等.集合中包含一组不重复出现且无特性顺序的元素. HashSet<T>的一些特性如下: 1 ...

  9. label swift

    var label = UILabel(frame: CGRectMake(10, 20, 300, 100)) label.text = "<Swift语言实战入门>" ...

  10. Object:

    所有类的直接或者间接父类,Java认为所有的对象都具备一些基本的共性内容,这些内容可以不断的向上抽取,最终就抽取到了一个最顶层的类中的,该类中定义的就是所有对象都具备的功能. 具体方法: 1,bool ...