反字典序输出整数所有分割方式
来源:互联网 发布:淘宝知名女模 编辑:程序博客网 时间:2024/03/29 17:24
来源是《C语言名题精选百则》
算法思想是:
(1)每次从数中抽取一个残留数sum。如果结尾不是1,sum为1,如果结尾是1,sum是所有1的和加1.
(2)抽取sum后原来的数变成size。再用size去分sum。
(3)sum%size作为最后一个数。
/* ------------------------------------------------------ *//* 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 20void 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",count);system("pause");}/* ------------------------------------------------------ *//* 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 */}
0 0
- 反字典序输出整数所有分割方式
- 整数的分割方式
- 整数不同分割方式
- 整数的所有分割数目
- 输出正整数的所有分割
- 集合的所有分割方式
- 集合的所有分割方式
- 输出整数的所有分解
- 输出整数的所有划分
- 最长路及其字典序输出所有解
- 整数的所有不同分割数目
- 整数的所有不同分割数目
- 字符(0~9)取反整数输出
- 问题3.9 整数的分割方式
- 整数分解,输出所有分解方案
- 输出一个整数的所有数字
- 输出一个整数的所有因子
- [google interview] 输出所有的整数
- 浏览器缓存:expires Cache-Control Last-Modified Etag
- Linux配置网络出现Eroor adding default gateway的解决方案
- 【手册】Linux User's Manual
- matlab 局部特征检测与提取(问题与特征)
- Caffe代码导读(1):Protobuf例子
- 反字典序输出整数所有分割方式
- HTTP报头含义
- (1)
- Android应用优化之业务优化
- 数字分解
- 程序员面试金典——魔术索引1
- 文章标题
- 免费微软、兰亭、叶根友、圆体字体
- LeetCode之Range Sum Query