189 8069 5689

二进制求子集

#include
#include
using namespace std;
vector > result;
int cnt;
void solution(vector nums)
{
int num=1;
    cnt = (1 << nums.size());
    for(int i = 0; i < cnt; i++)
    {
        vector item;
        for(int j = 0; j < nums.size(); j++)
        {
            if(i & (1 << j))
            {
                item.push_back(nums[j]);
            }
        }
        result.push_back(item);
    }
}
int main()
{
    vector nums;
    nums.push_back(1);
    nums.push_back(2);
    nums.push_back(3);
     solution(nums);
     for(int i = 0; i < cnt; i++)
     {
        if(result[i].size() == 0)
        {
            cout<<"[]";
         }
        for(int j = 0; j < result[i].size(); j++)
        {
            cout<<"["<

当前标题:二进制求子集
文章分享:http://cdxtjz.cn/article/pjjgic.html