C++ 在c+中的3个数组中找到所有可能的解决方案,而不重复+;

C++ 在c+中的3个数组中找到所有可能的解决方案,而不重复+;,c++,arrays,C++,Arrays,我有3个数组,我需要得到这3个数组的和。这是我的数组 [37,9,7] [42,50,2] [57,92,52] 我需要不重复地找到所有可能的解决办法。 在这种情况下,我需要找到6种解决方案,它们是, 37+50+52,9+2+57,7+50+57,9+42+52,37+92+2,7+42+92 这是我的代码: #include <iostream> using namespace std; int main() { int arr[3] = { 37, 9, 7 };

我有3个数组,我需要得到这3个数组的和。这是我的数组
[37,9,7]
[42,50,2]
[57,92,52]
我需要不重复地找到所有可能的解决办法。 在这种情况下,我需要找到6种解决方案,它们是,
37+50+52,9+2+57,7+50+57,9+42+52,37+92+2,7+42+92

这是我的代码:

#include <iostream>

using namespace std;

int main()
{
    int arr[3] = { 37, 9, 7 };
    int arr1[3] = { 42, 50, 2 };
    int arr2[3] = { 57, 92, 52 };
    int arr3[3] = {};

    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 3; j++)
            for (int k = 0; k < 3; k++)
            {
                if (i != j && i!= k && j !=k)
                    arr3[i] = arr[i] + arr1[j] + arr2[k];
            }

    cout << "The sum is " << endl;
    for (int i = 0; i < 6; i++)
    {
        cout << arr3[i] << " " << endl;
    }

    return 0;
}

int arr3[3]={}用于保存所有解决方案,但只有3个元素。您只需设置这3个元素

也许您应该将大小增加到您期望的解决方案数量,并为
arr3
保留一个单独的计数器,因为
i
将只从0变为2


实际代码留作练习。

intarr3[3]={}用于保存所有解决方案,但只有3个元素。您只需设置这3个元素

也许您应该将大小增加到您期望的解决方案数量,并为
arr3
保留一个单独的计数器,因为
i
将只从0变为2


实际代码留作练习。

@Luchian的答案是正确的,切中要害。然而,你应该尝试学习现代C++,即使你是初学者。不幸的是,大多数教师/讲师都把它作为“高级主题”,而不应该是这样。编写现代C++意味着(大部分时间)充分利用标准库。大多数情况下,您的代码会更安全。这是我如何使用标准库中的容器和算法实现的:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    const std::size_t N = 3; // number of arrays
    std::vector<std::vector<int>> arrays;

    arrays.push_back({ 37, 9, 7 }); // first array
    arrays.push_back({ 42, 50, 2 }); // second array
    arrays.push_back({ 57, 92, 52 }); // third array

    std::vector<int> result; // here we store the results

    std::vector<std::size_t> perm(N);
    // fill in the vector with 0 ... N-1, which we'll later permute
    std::iota(perm.begin(), perm.end(), 0); 
    do { // loop over all permutations
        int sum = 0;
        for (std::size_t i = 0; i < perm.size(); ++i)
            sum += arrays[i][perm[i]];
        result.push_back(sum);
    } while (std::next_permutation(perm.begin(), perm.end()));

    // display the result
    std::cout << "The possible sums are: " << std::endl;
    for(auto elem: result)
        std::cout << elem << " ";
}
#包括
#包括
#包括
int main()
{
const std::size\u t N=3;//数组数
向量阵列;
array.push_back({37,9,7});//第一个数组
array.push_back({42,50,2});//第二个数组
array.push_back({57,92,52});//第三个数组
std::vector result;//我们在这里存储结果
std::向量perm(N);
//用0…N-1填充向量,我们稍后将对其进行置换
std::iota(perm.begin(),perm.end(),0);
对所有置换进行{//循环
整数和=0;
对于(std::size_t i=0;iLuchian:答案是正确的,切中要害。但是,即使你是初学者,你也应该尝试学习现代C++。不幸的是,大多数老师/老师把它当作一个“高级话题”,而不是原来的。写现代C++意味着(大部分时间)。充分利用标准库。在大多数情况下,您的代码会更加安全。这就是我如何使用标准库中的容器和算法:

#include <iostream>
#include <algorithm>
#include <string>

int main()
{
    const std::size_t N = 3; // number of arrays
    std::vector<std::vector<int>> arrays;

    arrays.push_back({ 37, 9, 7 }); // first array
    arrays.push_back({ 42, 50, 2 }); // second array
    arrays.push_back({ 57, 92, 52 }); // third array

    std::vector<int> result; // here we store the results

    std::vector<std::size_t> perm(N);
    // fill in the vector with 0 ... N-1, which we'll later permute
    std::iota(perm.begin(), perm.end(), 0); 
    do { // loop over all permutations
        int sum = 0;
        for (std::size_t i = 0; i < perm.size(); ++i)
            sum += arrays[i][perm[i]];
        result.push_back(sum);
    } while (std::next_permutation(perm.begin(), perm.end()));

    // display the result
    std::cout << "The possible sums are: " << std::endl;
    for(auto elem: result)
        std::cout << elem << " ";
}
#包括
#包括
#包括
int main()
{
const std::size\u t N=3;//数组数
向量阵列;
array.push_back({37,9,7});//第一个数组
array.push_back({42,50,2});//第二个数组
array.push_back({57,92,52});//第三个数组
std::vector result;//我们在这里存储结果
std::向量perm(N);
//用0…N-1填充向量,我们稍后将对其进行置换
std::iota(perm.begin(),perm.end(),0);
对所有置换进行{//循环
整数和=0;
对于(std::size_t i=0;istd::cout因为你不想重复,你可以考虑在所有排列上循环,而不是在维度数组上循环
N
循环
N
。然后将复杂性从
N^N
降低到
N!
。这是否意味着我应该创建另一个函数并将所有排列放入数组和loo中p结束吗?既然你不想重复,你可以考虑在所有排列上循环,而不是在维度数组上的
N
循环
N
。然后你会将复杂性从
N^N
降低到
N!
。这是否意味着我应该创建另一个函数,并将所有排列放入数组和loo中p结束吗?这是一个很好的解决方案(加上一个),尽管您可以通过使用
无符号的
来防止
int
溢出的未定义行为。那么它将是真正安全的。谢谢。您介意解释一下(auto-elem:result)的
行吗
?@Chow这是C++11及更高版本推出的基于
的新系列。它有点“复制”Python用于范围循环。有关更多详细信息,请参阅。
auto
只是告诉编译器推断类型,因此您可以使用
auto-elem:result
const-auto&elem:result
通过引用进行传递,而不是使用
int-elem:result
@Bathsheba确实如此,谢谢你的评论。我只是使用了OP类型。一般来说,在编写此类算法时,我更喜欢将其模板化。当然,处理溢出会变得很棘手,大多数时候我并不在意(除非问题变得严重,例如计算行列式)(一个很好的解决方案(加上一个)虽然你可以通过使用
无符号的
来防止
int
溢出的未定义行为,但它确实是安全的。谢谢。你介意解释一下(auto-elem:result)的
行吗
?@Chow这是C++11及更高版本推出的基于
的新系列。它有点“复制”Python用于范围循环。有关更多详细信息,请参阅。
auto
只是告诉编译器推断类型,因此您可以使用
auto-elem:result
const-auto&elem:result
通过引用进行传递,而不是使用
int-elem:result
芭丝谢芭