求集合的子集

利用递归算法

#include <iostream>
#include<string>
using namespace std;
void  trail(string[],int[],int,int);
int main()
{
 int n,k=0;
 cout<<"Please enter the number of the element :";
 cin>>n;
 string a[100];
 int b[100]={0};
 for (int i=0;i<n;i++)
 {
  cout<<"Please enter NO."<<i+1<<" element :";
  cin>>a[i];
 }
 cout<<"All of the subset is:"<<endl;
 trail(a,b,0,n);
 return 0; 
}
void trail(string a[],int b[],int k,int n)
{
 int j;
 if (k<n)
 {
  trail(a,b,k+1,n);
  b[k]=1-b[k];
  trail(a,b,k+1,n);
 }
 else
 {
  cout<<"{";
  for (j=0;j<=n;j++)
  {
   if (b[j])
   {
    cout<<a[j]<<" ";
   }
  }
  cout<<"} ";
 }
 
}
 


posted @ 2015-05-21 12:24  Tovi  阅读(147)  评论(0编辑  收藏  举报