经典名题2----字典顺序列出集合的子集
2007-06-05 12:58
288 查看
/* ------------------------------------------------------ */
/* PROGRAM All Possible Subset by Lexical Order : */
/* This program generates all subsets by lexical order.*/
/* */
/* Copyright Ching-Kuang Shene July/05/1989 */
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define LOOP 1
void main(void)
{
int set[MAXSIZE];
int n, i;
int position;
char line[100];
printf("/nAll Possible Subsets Generation by Lexical Order");
printf("/n================================================");
printf("/n/nNumber of Elements in the Set --> ");
gets(line);
n = atoi(line);
printf("/n{}"); /* the empty set */
position = 0; /* start from the 1st pos. */
set[position] = 1; /* it gets a '1' */
while (LOOP) { /* loop until done... */
printf("/n{%d", set[0]); /* print one result */
for (i = 1; i <= position; i++)
printf(",%d", set[i]);
printf("}");
if (set[position] < n) { /* this pos. can be inc*/
set[position+1] = set[position] + 1; /* YES*/
position++; /* inc. next pos. */
}
else if (position != 0) /* NO, the 1st pos? */
set[--position]++; /* backup and increase */
else /* NO, the 1st pos and can */
break; /* not be inc. JOB DONE! */
}
}
/* PROGRAM All Possible Subset by Lexical Order : */
/* This program generates all subsets by lexical order.*/
/* */
/* Copyright Ching-Kuang Shene July/05/1989 */
/* ------------------------------------------------------ */
#include <stdio.h>
#include <stdlib.h>
#define MAXSIZE 20
#define LOOP 1
void main(void)
{
int set[MAXSIZE];
int n, i;
int position;
char line[100];
printf("/nAll Possible Subsets Generation by Lexical Order");
printf("/n================================================");
printf("/n/nNumber of Elements in the Set --> ");
gets(line);
n = atoi(line);
printf("/n{}"); /* the empty set */
position = 0; /* start from the 1st pos. */
set[position] = 1; /* it gets a '1' */
while (LOOP) { /* loop until done... */
printf("/n{%d", set[0]); /* print one result */
for (i = 1; i <= position; i++)
printf(",%d", set[i]);
printf("}");
if (set[position] < n) { /* this pos. can be inc*/
set[position+1] = set[position] + 1; /* YES*/
position++; /* inc. next pos. */
}
else if (position != 0) /* NO, the 1st pos? */
set[--position]++; /* backup and increase */
else /* NO, the 1st pos and can */
break; /* not be inc. JOB DONE! */
}
}
相关文章推荐
- 列出所有子集-----字典顺序 2013年1月14日
- 列出所有子集-----字典顺序 2013年1月14日
- 列出所有子集(字典顺序)
- 集合所有子集,无字典顺序与有字典顺序的两种解法(C/OC)
- 每日一小练——按字典顺序列出所有子集
- 每日一小练——按字典顺序列出全部子集
- 列出所有子集-----字典顺序 2013年1月14日
- [经典算法] 排列组合-N元素集合的M元素子集
- [经典算法] 排列组合-N元素集合的所有子集(二)
- 集合习题之列出有限集合所有子集
- 005_012 Python 检查序列的成员 in影响性能,而且不能转换为字典或者集合,需要保留原来的顺序
- 每日一小练——按字典顺序列出所有排列
- [经典算法] 排列组合-N元素集合的所有子集(一)
- 每日一小练——按字典顺序列出全部排列
- 在一个集合中存储了无序并且重复的字符串,定义一个方法,让其有序(字典顺序),而且还不能去除重复
- 集合的排序 一个集合中存储无序元素 使其有序化(字典顺序) 不去重
- 列出所有子集(格雷码顺序)
- 在一个集合中存储了无序并且重复的字符串,定义一个方法,让其有序(字典顺序),而且还不能去除重复
- 每日一小练——按字典顺序列出全部排列
- C语言名题精选百则:所有子集,字典子集,Gray子集