Brute force method to solve the problem of generating subsets (algorithm initial learning)

Source: Internet
Author: User
Tags pow

on the problem of generating subsets by brute force method

For the problem of algorithm analysis I have no wish to say anything to myself, to the junior, unexpectedly still small white. I studied the problem of generating subsets for the whole afternoon and finally figured it out.
Idea: 1. Use the POW (x, y) function in the <math.h> header file to obtain the N-order of 2 as an outer loop. 2. Then write a function that converts the 10 binary number to the 2 binary number (of course, C has a special conversion function in <stdlib.h>, however, he converted 5 is not 0101 but 101, which leads to the need to separate judgment, so do not itoa function).
As follows:
Conversation (int n,int B[max])
{
int i=0;
for (i;i<max;i++)
{
b[i]=n%2;
N=N/2;
if (n==0)
Break
}
}
3. Use the above function to convert to 2, then go back to the loop body and use the loop to output a subset of each.
The complete code is as follows:
#include <iostream>
#include <stdlib.h>
#include <String>
using namespace Std;
#define MAXSIZE 10
void conversion (int n,int b[])
{
int i=0;
for (i;i<maxsize;i++)
{
b[i]=n%2;
N=N/2;
if (n==0)
Break
}
}
void Main ()
{
int n;
cout<< "Please enter a subset of the generation of several numbers: \ n";
cin>>n;
if (n<=maxsize)
{
int b[maxsize];
BOOL flag = FALSE;
for (int i=0;i<pow (2,n); i++)
{
conversion (I,B);
for (int j=0;j<maxsize;j++)
{
if (b[j]==1)
{
cout<<j+1;
Flag = true;
}
}
if (!flag)
cout<< "Empty";
cout<<endl;
}
}
Else
cout<< "The number is too big \ n";
}
Finish this feel more should take out time to study the algorithm, so simple design is not come out, how the future will be successful!!!
Come on!!!!
Conversation (intNintB[max]) {      intI=0;  for(i;i<max;i++) {B[i]=n%2; N=n/2; if(n==0)              Break; }} 3use the above function to convert to 2, then go back to the loop body and use the loop to output a subset of each. The complete code is as follows: #include<iostream>#include<stdlib.h>#include<String>using namespacestd;#defineMAXSIZE 10voidConversionintNintb[]) {    intI=0;  for(i;i<maxsize;i++) {B[i]=n%2; N=n/2; if(n==0)    Break; }}voidMain () {intN; cout<<"Please enter a subset of the builds that are several numbers: \ n"; Cin>>N;if(n<=MAXSIZE) {   intB[maxsize]; BOOLFlag =false;  for(intI=0; I<pow (2, n); i++) {conversion (I,B);  for(intj=0; j<maxsize;j++)  {   if(b[j]==1) {cout<<j+1; Flag=true; }  }  if(!flag) cout<<"Empty"; cout<<Endl; } } Elsecout<<"the numbers are too big \ n";}
View Code

Brute force method to solve the problem of generating subsets (algorithm initial learning)

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.