正文

SETPART.C(转载)2006-06-12 09:12:00

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

分享到:

/* ------------------------------------------------------ *//* FUNCTION  set_partition :                              *//*    Given an integer n, this function will lists all    *//* possible partitions of {1,2,3,...,n}.                  *//*                                                        *//* Copyright Ching-Kuang Shene               July/24/1989 *//* ------------------------------------------------------ */ #include <stdio.h>#include <stdlib.h>           /* for malloc()             */ #define  ALWAYS   1 void  display(int *, int); void  set_partition(int n){     int  *code, *maxi;       /* arrays for code[], maxi[]*/     int  i, j;      code = (int *) malloc(sizeof(int)*n); /* get memory  */     maxi = (int *) malloc(sizeof(int)*n);      for (i = 0; i < n; i++)  /* initialization           */          code[i] = 1, maxi[i] = 2;      while (ALWAYS) {         /* loop until done.         */          display(code, n);   /* display one partition    */          for (i = n-1; code[i] == maxi[i]; i--)               ;              /* find next 'increasible'  */          if (i > 0) {        /* found ?                  */               code[i]++;     /* YES, update              */               for (j = i + 1; j < n; j++) {                    code[j] = 1;                     maxi[j] = maxi[i]+((code[i]==maxi[i]) ? 1 : 0);               }          }          else                /* NOT FOUND, done.         */               break;     }     free(code);     free(maxi);} /* ------------------------------------------------------ *//* FUNCTION  display :                                    *//*    This function displays the code of the partition.   *//* ------------------------------------------------------ */ void  display(int *code, int n){     int  i;        printf("\n");     for (i = 0; i < n; i++)          printf("%3d", *(code+i));} /* ------------------------------------------------------ */ void main(void){     char  line[100];     int   n;      printf("\nSet Partition Program for {1,2,3,...,N}");     printf("\n=======================================");     printf("\n\nN Please --> ");     gets(line);     n = atoi(line);     printf("\nCodes of Partitions.");     printf("\ni-th position = j means i in partition j\n");     set_partition(n);}

阅读(1965) | 评论(0)


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

评论

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