如何在c ++中获取数组中所有组合的产品总和? [英] How to get sum of products of all combinations in an array in c++?

查看:92
本文介绍了如何在c ++中获取数组中所有组合的产品总和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++,我给了一个像= [1,2,3,4]的数组,我想找到所有可能的组合乘法的总和,如:



对于1:1 + 2 + 3 + 4的组合



对于2:1 * 2 + 2 * 3 + 3 * 4的组合+ 4 * 1.



组合3:1 * 2 * 3 + 1 * 3 * 4 + 2 * 3 * 4



对于4:1 * 2 * 3 * 4的组合



最后所有这些总和的总和就是我的答案。

I'm using C++ and I'm given an array like a = [1, 2, 3, 4] and I want to find sum of all possible combination multiplications like:

For combinations of 1: 1 + 2 + 3 + 4

For combinations of 2:1*2 + 2*3 + 3*4 + 4*1.

For combination of 3: 1*2*3 + 1*3*4 + 2*3*4

For combinations of 4: 1*2*3*4

And finally sum of all these sums is my answer.

推荐答案

看起来非常像我的作业。



一个直接的方法可以完成这项工作,但是不是真正最好的方法。



1.手动找到所有可能的组合。

2.编写每个组合的代码来做总和。

3.总结所有金额并打印(或者你想做什么)



只有你知道数组中的元素数量并不是特别优雅,但如果需要的话快速完成工作你就可以这样做。

但是,这是一个很好的步骤,可以使用有限的元素,可以用来验证你的答案。



更好的方法是找出如何在循环中完成它并在循环中为每个组合设置一个案例。它适用于数组中的任意数量的元素。

诀窍是为每种情况找到正确的算法。
Looks very much like homework to me.

A straight forward approach would do the job, but is not really the best method.

1. Manually find all possible combinations.
2. Write the code for each combination to do the sum.
3. Sum all sums and print (or whatever it is you want to do)

This works only if you have a known number of elements in the array and it is not particularly elegant, but if you need to finish the work quickly you can do it that way.
However, this is a good step to do with a limited amount of elements and can be used to verify your answers later on.

A better approach is to find out how to do it in a loop and have one case for each combination in the loop. It will work for any number of elements in an array.
The trick is to find the correct algorithm for each case.


我要说的关键是把这个问题放到代码中是从一组M数中找到并迭代N个元素的每个子集。我不打算提供完整的解决方案,因为这似乎是功课,但这就是我要去做的事情:



1.定义一个函数选择前N个元素并将它们添加到容器或合适的结果类型(struct,无论如何)

2.定义一个函数,给定最后一个子集,生成下一个元素子集(即为什么在步骤1)中明确地存储整个数字子集是很重要的。显然,您需要考虑一种从前一个子集导出新子集的方法,以便遍历所有可能的组合。

3.定义一个函数来计算给定子集中的乘积和(可能在一个单独的函数中)累积所有产品。

4.定义一个函数通过I / O查询问题参数,或者从命令行读取它们

5.定义将上述所有内容绑定在一起的主要功能。



我建议你首先尝试一下只有1和2个数字的简单程序,你在哪里可以手动计算并验证预期结果。这将帮助你清除琐碎的错误。
I'd say the key to put this problem into code is finding and iterating over each subset of N elements from a set of M numbers. I'm not going to deliver a full solution, since this appears to be homework, but this is how I'd go about it:

1. Define a function that picks the first N elements and add them to a container or suitable result type (struct, whatever)
2. Define a function that, given the last subset, produces the next subset of elements (that is why it is important to store the entire subset of numbers explicitely in step 1). Obviously you need to think of a way to derive a new subset from the previous one in such a way that you will traverse all possible combinations.
3. Define a function to calculate the product from a given subset, and (possibly in a separate function) to cumulate all products.
4. Define a function to query the problem parameters via I/O, or read them from command line
5. Define the main function that binds all of the above together.

I suggest you first try the program on trivial sets of just 1 and 2 numbers, where you can manually calculate and verify the expected results. This will help you weed out trivial errors.


最难的部分是生成组合,这通常是通过递归方法完成的。 Stack Overflow上的一个很好的解释:创建所有可能的k C ++中的n个项目的组合 [ ^ ]。
The hardest part is generating the combinations, that's usually done with a recursive approach. A nice explanation at Stack Overflow: "creating all possible k combinations of n items in C++"[^].


这篇关于如何在c ++中获取数组中所有组合的产品总和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆