所有不同偶数子集的计数[关闭]

问题描述 投票:-1回答:1

所以我有一个程序,可以找到不同偶数的所有子集的计数,但我不断收到这个错误:“'{'token'之前预期的unqalified-id。这是我的代码:

#include <iostream>
#include <bits/stdc++.h>

using namespace std;
{
        int countSubsets(int array[], int n)
        unordered_set<int> us;
        int even_count = 0;

    {
        for(int i=0; i<n; i++)
            if(array[i] % 2 == 0)
                us.insert(array[i]);

        unordered_set<int>:: iterator itr;
        for(itr=us.begin(); itr!=us.end(); itr++)
            even_count++;

        return(pow(2, even_count) - 1);
    }
    int main()
    {
        int array[] = {4, 2, 1, 9, 2, 6, 5, 3};
        int n = sizeof(array) / sizeof(array[0]);
        cout << "Number of subsets = "
            << countSubsets(array, n);
        return 0;
    }
}

有什么建议?

c++
1个回答
1
投票

为什么你不应该使用#include <bits/stdc++.h>using namespace std;

你必须用花括号{...}将函数的主体边界。在您的示例中有两个功能:

int countSubsets(int array[], int n) {
    FUNCTION_BODY
}

int main() {
    FUNCTION_BODY
}

在您的情况下,没有必要在这些功能之外使用更多花括号。修复此错误后,删除using namespace std;#include <bits/stdc++.h>

#include <iostream>
#include <unordered_set>
#include <cmath>

int countSubsets(int array[], int n) {
    std::unordered_set<int> us;
    int even_count = 0;

    for (int i = 0; i < n; i++)
        if (array[i] % 2 == 0) {
            us.insert(array[i]);
        }

    for (std::unordered_set<int>::iterator  itr = us.begin(); itr != us.end(); itr++)
        even_count++;

    return(std::pow(2, even_count) - 1);
}

int main() {
    int array[] = {4, 2, 1, 9, 2, 6, 5, 3};
    int n = sizeof(array) / sizeof(array[0]);
    std::cout << "Number of subsets = "
        << countSubsets(array, n);
    return 0;
}
© www.soinside.com 2019 - 2024. All rights reserved.