正文

INTPART#.C(转载)2006-06-12 09:08:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/tankboy/15746.html

分享到:

/* ------------------------------------------------------ */
/* FUNCTION  int_part_no :                                */
/*    Given an integer n, this function computes the      */
/* number of partitions of the input integer.  NOTE that  */
/* this function computes the number only and does not    */
/* generate the corresponding partition.  For the second  */
/* purpose, see program INTPART.C please.                 */
/*                                                        */
/* Copyright Ching-Kuang Shene               July/23/1989 */
/* ------------------------------------------------------ */

unsigned long  compute(int, int);
unsigned long  int_part_no(int);

unsigned long  int_part_no(int n)
{
     return compute(n, n);    /* convert to another form */
}

/* ----------------------------------------------------- */
/* FUNCTION  compute :                                   */
/*    The computation routine.  It accepts number-the    */
/* number to be partitioned, and maximum-the maximum     */
/* value in any partition of number, then returns the    */
/* number of partitions subject to number and maximum.   */
/* ----------------------------------------------------- */

unsigned long  compute(int number, int maximum)
{
     if (number == 1 || maximum == 1)
          return 1;
     else if (number < maximum)
          return compute(number, number);
     else if (number == maximum)
          return 1 + compute(number, maximum-1);
     else
          return compute(number,maximum-1) +
                 compute(number-maximum,maximum);
}

/* ------------------------------------------------------ */

#include  <stdio.h>
#include  <stdlib.h>

void  main(void)
{
     int  n;
     char line[100];

     printf("\nNumber of partitions of an Integer");
     printf("\n==================================");
     printf("\n\nN --> ");
     gets(line);
     n = atoi(line);
     printf("\nThere are %lu partitions.", int_part_no(n));
}


阅读(1831) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册