列出所有子集(字典顺序)
2011-11-14 10:36
295 查看
#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! */
}
}
#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日
- 列出所有子集-----字典顺序 2013年1月14日
- 每日一小练——按字典顺序列出所有子集
- 每日一小练——按字典顺序列出全部子集
- 经典名题2----字典顺序列出集合的子集
- 每日一小练——按字典顺序列出所有排列
- 集合所有子集,无字典顺序与有字典顺序的两种解法(C/OC)
- 列出所有子集(格雷码顺序)
- 列出所有排列(字典顺序)
- 每日一小练——按字典顺序列出全部排列
- 每日一小练——列出所有子集
- 列出所有子集
- 列出所有的子集
- 集合习题之列出有限集合所有子集
- 产生所有排列---字典顺序-----2013年1月23日
- 对输入的字符串按字典顺序输出所有的全排列,字符串可以由重复字符
- 产生所有排列---字典顺序-----2013年1月23日
- 产生所有排列---字典顺序-----2013年1月23日
- 编写程序,循环接收用户从键盘输入多个字符串,直到输入“ end”时循环结束,并将所有 已输入的字符串按字典顺序倒序打印。