Power set [set theory]

Source: Internet
Author: User

The power -S,s of a well-known set is a collection of all subsets of the set S, denoted by P (S), for example:

P ({0,1,2}) ={$,{0},{1},{2},{0,1},{  0,2},{1,2},{0,1,2}};

This reminds me of the binary simulation, what if we now use the algorithm to simulate the set of print powers (except the empty sets)?
Binary simulation plays a very good role, first look at what binary simulation is,

The array is initially 0, loop in the first +1, and carry in binary format, and you end up with the following possibilities:

1 0 0) 0 0
0 1 0) 0 0
1 1 0 0 0 ...
If there are 3 elements, the combination has 7 forms, plus an empty set, that is, all the possibilities of the Power collection!

Full code

#include <iostream>using namespacestd; intMain () {intcarry=1; inta[5]={0};/*State Array*/    intb[5]={0,1,2};/*Collection Array*/    intn=7;  while(n>0)    {                 for(intI=0;i<5; i++)/*get a state every time*/{A[i]+=carry; Carry=a[i]/2; A[i]=a[i]%2; if(carry==0)             Break; }         for(intj=0;j<5; j + +)/*traversing array state, printing power set*/        {            if(a[j]==1) cout<<b[j]<<" "; } cout<<Endl; --N; Carry=1;/*Reset*/    }    return 0;}

Power set [set theory]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.