ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

集合所有子集

2021-11-18 20:35:19  阅读:112  来源: 互联网

标签:ListLength Buffer 所有 List char int 子集 集合 include


//我的方法
#include <iostream>
#include <vector>
#include <stack>
#include <set>
#include <string>
#include <algorithm>
using namespace std;
void fun(vector<int> &a, int i)
{
     if (i == 0)
     {
          cout << '@' << endl;
          return;
     }
     int k = 0;
     while (i)
     {
          if (i % 2)
          {
               cout << a[k] << ' ';
          }
          k++;
          i /= 2;
     }
     cout << endl;
}
int main(void)
{
     vector<int> a = {1, 2, 3};
     for (int i = 0; i < pow(2, a.size()); i++)
     {
          fun(a, i);
     }
}
== == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == == =
#include <iostream>
                                                                                                                                                                   using namespace std;
const int ListLength = 10;
// const int ListLength=3;

//输出Buffer集合
void Output(char *Buffer, int flag)
{
     static int count = 1;
     if (count == 1)
     {
          cout << count++ << ": { }" << endl;
     }
     cout << count++ << ": {";
     for (int i = 0; i <= flag; i++)
     {
          cout << Buffer[i];
     }
     cout << "}" << endl;
}
//找到元素c在集合List中的位置
int Index(char *List, char c)
{
     for (int i = 0; i <= ListLength - 1; i++)
     {
          if (c == List[i])
          {
               return i;
               break;
          }
     }
     return -1;
}

void SubSet(char *List, int m, char *Buffer, int flag)
{
     if (m <= ListLength - 1)
     {
          /*if(m==0)
          {
                  Buffer[0]=List[0];
          }*/
          // Buffer[flag]=List[m];
          /*if(flag==0)
          {
                Buffer[flag]=List[m];
          }*/

          for (int i = (flag == 0) ? 0 : Index(List, Buffer[flag - 1]) + 1; i <= ListLength - 1; i++)
          //当flag==0时,Buffer中没有任何元素,此时i=[0...ListLength-1]
          //当flag>0时,找到Buffer中的最后一个元素在集合List中的位置i,把[i....ListLength-1]
          //处的元素,加到Buffer元素的最后面
          {
               Buffer[flag] = List[i];
               Output(Buffer, flag);
               SubSet(List, m + 1, Buffer, flag + 1);
          }
     }
     return;
}

int main()
{
     char List[ListLength] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'};
     // char List[ListLength]={'a','b','c'};
     char Buffer[ListLength] = {' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '};
     // char Buffer[ListLength]={' ',' ',' '};
     // int flag=0;
     // TEST
     // cout<<Index(List,'c'); OK
     SubSet(List, 0, Buffer, 0);
     system("pause");
     return 0;
}

标签:ListLength,Buffer,所有,List,char,int,子集,集合,include
来源: https://www.cnblogs.com/zupernova/p/15574198.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有