lzmagic  
1/4桶水荡漾
日历
<2008年9月>
31123456
78910111213
14151617181920
21222324252627
2829301234
567891011
统计
  • 随笔 - 7
  • 文章 - 0
  • 评论 - 0
  • 引用 - 0

导航

常用链接

留言簿

随笔分类

随笔档案

搜索

  •  

最新评论

阅读排行榜

评论排行榜

 
/*
 * 正整数划分问题:
 * 将正整数n表示成一系列正整数之和,n = n1 + n2 +……+ nk (n1 >= n2 >=……>= nk >= 1, k >= 1)
 * 正整数n的这种表示称为正整数n的划分。
 * 求正整数n的不同划分数pn。
 * Input: n  (End with 0)
 * Ouput: pn (in each line)
 */

 1// Sulotion 1st:
 2#include<iostream>
 3using namespace std;
 4
 5int Count(int n, int m)
 6{
 7     if(m == 1return 1;
 8    if(n <  m) return Count(n, n);
 9    if(n == m) return 1 + Count(n, m - 1);
10    if(n >  m) return Count(n - m, m) + Count(n, m - 1);
11}

12
13int main()
14{
15    int n;
16    cin >> n;
17    while(n != 0)
18    {
19        cout << Count(n, n) << endl;
20        cin >> n;
21    }

22
23    return 0;
24}

 1// Solution 2nd:
 2#include<iostream>
 3using namespace std;
 4
 5#define MAXN 100
 6
 7int main()
 8{
 9    int n, a[MAXN][MAXN], max, i, j;
10
11    // 初始值
12    a[1][1= 1;
13    max = 1;
14
15    cin >> n;
16    while(n != 0)
17    {
18        if(n > max)
19        {
20            for(i = max + 1; i <= n; i++)
21            {
22                a[i][1= a[i - 1][1];
23                for(j = 2; j <= i / 2; j++)
24                    a[i][j] = a[i - j][j] + a[i][j - 1];
25                for(; j <= i - 1; j++)
26                    a[i][j] = a[i - j][i - j] + a[i][j - 1];
27                a[i][j] = 1 + a[i][j - 1];
28            }

29            max = n;
30        }

31        cout << a[n][n] << endl;
32        cin >> n;
33    }

34    return 0;
35}
posted on 2008-04-12 11:01 lzmagic 阅读(89) 评论(0)  编辑 收藏 引用 所属分类: practising

标题  
姓名  
主页
验证码 *
内容(提交失败后,可以通过“恢复上次提交”恢复刚刚提交的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
[使用Ctrl+Enter键可以直接提交]
相关链接:
网站导航:




 
Copyright © lzmagic Powered by: 博客园 模板提供:沪江博客