Thursday, February 6, 2014

show all subset of a set

Newsgroup: comp.lang.c++

Subject: show all subset of a set

From: mary8shtr@...

Date: Thu, 6 Feb 2014 08:13:54 -0800 (PST)



A program is a set of all subsets of this part of the show. Users can enter a number., For example, if n = 2 the output looks like this:

{}

{1}

{2}

{1,2}

or for n=3 we have:

{}

{1}

{2}

{3}

{1,2}

{1,3}

{2,3}

{1,2,3}

meantime this program should be solved with tow way.

recursive function and Non-recursive.Can also be used in solving the problem of bitwise operations.



my algorithm that is for per subset exists one number. these numbers are form 0 until (2^n)-1.allow me explain it with a example.

n=3

{ } 000 0

{1} 100 4

{2} 010 2

{3} 001 1

{1,2} 110 6

{1,3} 101 5

{2,3} 011 3

{1,2,3} 111 7

The third column show some number(fn) that i use in code.now we know per number of subset has to state(sn).0 or 1. this means it exists or not exists.now i should match fn and sn.and suggested way is use of bitwise operators.(operator &).and now i don't know what do and thing with else thing.

#include <iostream>

using namespace std;

#include <conio.h>

#include <string.h>

int pow(int a,int b)

{

int p=1;

for(int i=0;i<b;i++)

p=p*a;

return p;

}

struct ar

{

int binary;

int content;

};

int main()



{

int testcase=0;

int n;

ar *a;

a=new ar [2000];

cin>>testcase;

int i=0;

while(i<testcase)

{

cin>>n;

for(int j=0,k=1;j<n;j++,k++)

{

a[j].content=k;

a[j].binary=2;

}





for(int p=0;p<pow(2,n)-1;p++)

{

cout<<'{';

for(int m=0;m<n;m++)

{



int b;

b=a[m].binary&p;

if(b==1)

cout<<a[i].content<<' ';



}

cout<<'}';

}



cout<<endl;

i++;

}

return 0;

}







via Usenet Forums - Usenet Search,Free Usenet - comp.lang.c++ http://ift.tt/LVdQRq

View all the progranning help forums at:

http://ift.tt/1dP9txN

No comments:

Post a Comment