| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 985 人关注过本帖
标题:[讨论]整数划分问题
只看楼主 加入收藏
hejing1109
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-27
收藏
 问题点数:0 回复次数:3 
[讨论]整数划分问题

整数划分问题将一个整数划分为一系列正整数之和

例如P(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
要打印出 等式数量 和各个等式(像上面那样)

搜索更多相关主题的帖子: 正整数 
2006-11-19 03:03
我不是郭靖
Rank: 3Rank: 3
等 级:新手上路
威 望:6
帖 子:494
专家分:6
注 册:2006-10-4
收藏
得分:0 

给你一个参考程序(不是我写的)

/* ------------------------------------------------------ */
/* PROGRAM integer partition : */
/* Given a positive integer n, this program lists all */
/* partition of n in anti-lexical order. */
/* */
/* Copyright Ching-Kuang Shene July/21/1989 */
/* ------------------------------------------------------ */

#include <stdio.h>
#include <stdlib.h> /* for atoi() */

#define MAXSIZE 20

void display(int [], int [], int);

void main(void)
{
int partition[MAXSIZE+1]; /* the actuall partition */
int mult[MAXSIZE+1]; /* multiplicity */
int part_no; /* no. of parts */
int sum, size, remainder, count;
int n;
char line[100];

printf("\nPartition of Integer");
printf("\n====================");
printf("\n\nInput a Positive Integer --> ");
gets(line);
n = atoi(line);

partition[1] = n; /* at the biginning, we have*/
mult[1] = part_no = count = 1; /* only one part.*/
display(partition, mult, part_no);

do { /* size one sum in 'sum' */
sum = (partition[part_no]==1) ? mult[part_no--]+1 : 1;
size = partition[part_no] - 1; /* dec. size */
if (mult[part_no] != 1) /* last part with mul=1*/
mult[part_no++]--; /* NO, cut this part */
partition[part_no] = size; /* set new part=size */
mult[part_no] = sum/size + 1; /* fill other*/
if ((remainder = sum % size) != 0) {
partition[++part_no] = remainder;
mult[part_no] = 1;
}
count++;
display(partition, mult, part_no);
} while (mult[part_no] != n);
printf("\n\nThere are %d partitions in anti-lexical order\n",
count);
}


/* ------------------------------------------------------ */
/* FUNCTION display : */
/* This routine displays the given partition. */
/* ------------------------------------------------------ */

void display(int partition[], int mult[], int part_no)
{
int i, j;

printf("\n");
for (i = 1; i <= part_no; i++) /* for each part */
for (j = 1; j <= mult[i]; j++) /* and its mult. */
printf("%3d", partition[i]); /* show them */
}


2006-11-19 15:59
hejing1109
Rank: 1
等 级:新手上路
帖 子:28
专家分:0
注 册:2006-9-27
收藏
得分:0 
谢了 斑竹

2006-11-19 17:02
feitianyjx
Rank: 1
等 级:新手上路
帖 子:7
专家分:0
注 册:2007-5-10
收藏
得分:0 
do { /* size one sum in 'sum' */
sum = (partition[part_no]==1) ? mult[part_no--]+1 : 1;
size = partition[part_no] - 1; /* dec. size */
if (mult[part_no] != 1) /* last part with mul=1*/
mult[part_no++]--; /* NO, cut this part */
partition[part_no] = size; /* set new part=size */
mult[part_no] = sum/size + 1; /* fill other*/
if ((remainder = sum % size) != 0) {
partition[++part_no] = remainder;
mult[part_no] = 1;
}


我是菜鸟。这段代码我看了好久,还不是太明白,可以说下它的功能 吗?
2007-05-14 13:15
快速回复:[讨论]整数划分问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.018713 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved