列出所有子集(格雷码顺序)
来源:互联网 发布:java同步方法 编辑:程序博客网 时间:2024/05/19 01:08
#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*/
}
//output
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)*/
}
}
- 列出所有子集(格雷码顺序)
- 列出所有子集(字典顺序)
- 列出所有子集(二进制递增方式)
- 列出所有子集
- 每日一小练——按字典顺序列出所有子集
- 列出所有子集-----字典顺序 2013年1月14日
- 列出所有排列(字典顺序)
- 列出所有K个元素的子集
- 每日一小练——列出所有子集
- 列出{1,2,,,,n}的所有子集
- 列出一个集合的所有非空子集
- 列出所有子集----------2013年1月3日
- 列出所有子集(采用列出2进制数的方法)-------------2013年1月4日
- 集合所有子集,无字典顺序与有字典顺序的两种解法(C/OC)
- 所有子集
- 列出所有K个元素的子集-----2013年1月26日
- 每日一小练——按字典顺序列出所有排列
- Set Difference(所有子集的最值差)
- 手把手教你玩转SOCKET模型之重叠I/O篇
- 列出所有子集(字典顺序)
- (转)抽屉中关闭时默认显示一部分的内容
- 选中Edit控件中的文本
- vmware环境下的kgdb环境配置和模块调试示例 (1)
- 列出所有子集(格雷码顺序)
- GridView 的 onselectedindexchanged 方法
- Linux下用Apple的bonjour服务进行摄像机搜索
- 列出所有排列(旋转法)
- dwr下拉框默认值及保存选中的值
- Core Data
- 列出所有排列(字典顺序)
- java日期知识点整理及例子程序
- *****