Discrete Mathematics 2nd Chapter Computer and Exploration 2nd question

Source: Internet
Author: User

Problem: Given a finite set, try listing all the elements in its power set.

Workaround 1:to generate all subsets of s,list all 2n bit strings of length n (for instence, in increasing order), and write down T He corresponding subsets.

Workaround 2: Take collection <a,b,c,d> as an example

First step: Add an empty set,

The second step: adding a cell to a single-element subset, and storing it in a Tempbefore string array for conversion to a subset of two elements, the single-element subset in temp is a,b,c,d.

Step three: Add an element to each subset, generating a subset of the N+1 elements, using the Tempbefore and set S generated in the second step.

1 Private Static intChange (string[] tempafter, string[] Tempbefore,2 string[] first) {3         intTempafterlength = 0;4          for(inti = 0;!tempbefore[i].equals ("#"); i++) {5Tempafterlength =Addtempafter (Tempafter, Tempbefore[i], first,6 tempafterlength);7         }8         returntempafterlength;9}
View Code

Private Static intAddtempafter (string[] tempafter, string string, string[] First,inttempafterlength) {        intLocation =getlaststringlocation (First, string); if(First[location + 1].equals ("#")) {            returntempafterlength; /*** For example, if Tempbefore[i] is d!ef and the input string set is "v,<a,v>,d,ef#" * then we know that the subset D!ef cannot be regenerated into a new subset. * */        }         for(inti = location + 1;!first[i].equals ("#"); i++) {Tempafter[tempafterlength]= string + "!" +First[i]; Tempafterlength++; }        returntempafterlength; }
View Code

/** * Gets the position of the last element of the string in the input collection, such as the input set is "v,<a,v>,d,ef#", string v!d * Returns position 2, because element D is subscript 2 in the first array Where Str is taken as the last of the String in this example is D * */private static int getlaststringlocation (string[] first2, string string) {string str = "" ; int i = 0, location = 0;for (int j = 0; J < String.Length (); j + +) {str = str + string.charat (j); if (String.charat (j) = = '! ') {str = "";}} for (i = 0;!first2[i].equals ("#"), i++) {if (First2[i].equals (str)) {location = i;}} return location;}

  

Discrete Mathematics 2nd Chapter Computer and Exploration 2nd question

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.