子集合问题

  求集合S的所有子集合(不包括空集),可以按照框架一的算法写代码。

 1 #include<iostream>
 2 using namespace std;
 3 int s[4]={3,5,7,9};
 4 int x[4];
 5 int N=4;
 6 
 7 void print() {
 8     for(int j=0;j<N;j++)
 9      if(x[j]==1) 
10          cout<<s[j]<<" ";
11      cout<<endl;
12 }
13 
14 void SubSet(int i) {
15     if(i>=N) {
16         print();
17         return;
18     }
19     x[i]=1;
20     SubSet(i+1);
21     x[i]=0;
22     SubSet(i+1);
23 }
24 int main() {
25     cout<<"数组的子集合为: "<<endl;
26     SubSet(0); 
27     return 0;
28 }
View Code

相关文章:

  • 2022-12-23
  • 2021-06-10
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-10
  • 2021-05-03
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-03
  • 2021-11-17
相关资源
相似解决方案