编写一个程序,列出{1,2,3,…,n}这个集合的全部子集,包括空集合。git
列出一个集合的全部子集有不少作法,题目中并无要求依某个特定的次序来排列, 所以是不难作出来的。算法
由于集合中一共有n个元素,因此总共就会有2^n子集;例如{1,2,3}有以下子集:数组
{}this
{1} {2} {3}spa
{1,2} {1,3} {2,3}code
{1,2,3}字符串
看到2^n,就想起了二进制数,能够使用二进制的第 i 位表示是否包含集合的第 i 个元素,如数字6的二进制形式是110,表示取集合的第2,3两个元素组成的子集。这样0~2^n -1的数字就能够表示所有子集,每个数字表明一个子集,实现应该不难。get
#include <stdio.h> #include <stdlib.h> #define MAXSIZE 20 #define LOOP 1 int main(void) { char digit[MAXSIZE]; int i, j; int n; char line[100]; printf("\nDirect Generation of All Subsets of a Set"); printf("\n========================================="); printf("\n\nNumber of Elements in the Given Set --> "); gets(line); n = atoi(line);/*把字符串转换成整型数据*/ /* ---You'd better check to see if n is too large--- */ for (i = 0; i < n; i++) /* clear all digits to 0,能够认为是初始化,注意digit是字符数组 */ digit[i] = '0'; printf("\n{}"); /* outpout empty set {} ,空集是全部集合的子集 */ while (LOOP) { for (i = 0; i < n && digit[i] == '1'; digit[i] = '0', i++) ; /* find first 0 position 找到第一次出现0的位置,或者设置它为0 */ if (i == n) /* if none, all pos. are 1 */ break; /* thus all elem. are in set,这是在遍历完全部的元素以后,结束循环*/ else digit[i] = '1';/* now add one to this pos,设置该位置的元素为1 */ for (i = 0; i < n && digit[i] == '0'; i++) ; /* find first 1 position 找到第一次出现1的位置 */ printf("\n{%d", i + 1); /* show its numner and */ for (j = i + 1; j < n; j++) /* others,遍历后面的元素,找到标记为1的位置,打印出来该位置对应的数*/ if (digit[j] == '1') printf(",%d", j + 1); printf("}"); } return 0; }
当输入n=2的状况下,编译运行subset.c,实验结果以下:
it