正文

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

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

分享到:

/* ------------------------------------------------------ *//* PROGRAM Subset Generation by Gray Code :               *//*    This program generates all possible subsets of a n  *//* elements set with Gray Code.  Simply speaking, the Gray*//* code of n bits is a sequence of n-bit strings such that*//* any two consecutive two bit strings have exactly one   *//* position in difference.  For example, for n = 3, the   *//* gray code generated is the sequence 000,001,010,011,   *//* 010,110,111,101 and 100.  Therefore each item of this  *//* sequence corresponds one subset as in the direct method*//*                                                        *//* Copyright Ching-Kuang Shene               July/05/1989 *//* ------------------------------------------------------ */ #include  <stdio.h>#include  <stdlib.h> #define   MAXSIZE         20#define   YES             1#define   LOOP            1 #define   FLIP_DIGIT(x)   x = ((x) == '0' ? '1' : '0')#define   FLIP(x)         x = (1 - (x)) void main(void){     char  digit[MAXSIZE];     int   position[MAXSIZE];     int   even;     int   n;     int   i, count = 0;     char  line[100];      printf("\nAll Subset Listing by Gray Code");     printf("\n===============================");     printf("\n\nNumber of Elements in Set Please --> ");     gets(line);     n = atoi(line);      printf("\n");            /* initialization           */     for (i = 0; i < n; i++) {          digit[i] = '0';          printf("0");     }     printf(" : {}\n");       /* the empty set            */      even = YES;     while (LOOP)  {          if (even)           /* for even positions:0,2,..*/               FLIP_DIGIT(digit[0]);  /* flip the 1st bit */          else {              /* for odd positions...     */               for (i = 0; i < n && digit[i] == '0'; i++)                    ;         /* find the first 1 bit     */               if (i == n-1) break; /* if it is the last..*/               FLIP_DIGIT(digit[i+1]); /* NO, flip its nbr*/          }          for (count = 0, i = n - 1; i >= 0; i--) {               printf("%c", digit[i]); /* print the bits  */               if (digit[i] == '1')    /* and collect pos */                    position[count++] = i + 1;          }          printf(" : {%d", position[count-1]);          for (i = count - 2; i >= 0; i--) /* print pos   */               printf(",%d", position[i]);          printf("}\n");          FLIP(even);         /* next will be odd(or even)*/     }}

阅读(2743) | 评论(0)


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

评论

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