以下引用部分全都来自: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. 关于activity_main.xml与fragment_main.xml

    第一种解决办法 新版安装SDK文件一开始有两个XML文件,activity_main.xml和fragment_main.xml,不习惯的可以这样处理:1.删除fragment_main.xml整个文 ...

  2. 使用Telerik控件搭建Doubanfm频道部分

    今天感觉好累啊..还是坚持记录下. 收集的API: https://github.com/HakurouKen/douban.fm-api https://github.com/zonyitoo/do ...

  3. this的使用方法

    当一个对象创建后,Java虚拟机(JVM)就会给这个对象分配一个引用自身的指针,这个指针的名字就是this.this是指向对象本身的一个指针.this只和特定的对象关联,而不和类关联,同一个类的不同对 ...

  4. MAC下安装与配置MySQL [转]

    一 下载MySQL 访问MySQL的官网http://www.mysql.com/downloads/ 然后在页面中会看到“MySQL Community Server”下方有一个“download” ...

  5. C#.Net 图片处理大全

    C# How to: Image filtering by directly manipulating Pixel ARGB values C# How to: Image filtering imp ...

  6. iOS学习之UITabBarController

    一.标签视图控制器——UITabBarController 1.UITabBarController的继承关系: @interface UITabBarController : UIViewContr ...

  7. 引用类型a=b

    List<int> list = new List<int>(); list.Add(1); list.Add(2); list.Add(3); Cache["Key ...

  8. java数据结构和算法------选择排序

    package iYou.neugle.sort; public class Select_sort { public static void SelectSort(double[] array) { ...

  9. python 安装scrapy

    1. 首先你先得安装PYTHON...还是推荐2.7吧,之前装了3.3似乎和这个世界格格不入...先装个2.7. 并将python加入系统的环境变量. 2. 去scrapy 官网下载最新版本的scra ...

  10. Jsp实现筛选并压缩文件批量下载

    Jsp实现筛选并压缩文件批量下载 首先明确一下需求,网页端点击一下button,传递特定的参数到download.jsp网页,筛选文件,对过滤得到的文件进行压缩,然后返回前端一个压缩包下载. 以下的代 ...