经典名题2----字典顺序列出集合的子集

/* ------------------------------------------------------ */
/* 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!    */
     }
}
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值