C++ 在达到目标或达到数组末尾之前按顺序计算的数组元素总和

C++ 在达到目标或达到数组末尾之前按顺序计算的数组元素总和,c++,algorithm,logic,C++,Algorithm,Logic,假设我有一个数组: int A[6] = {1, 7, 2, 4, 3, 9}; int array[5] = { 500, 1000, 500, 500, 400 }; 我的目标号码是6。我们希望增加数字,直到达到目标或至少接近目标 排在第一位的数字具有更高的优先级,因此看起来如下所示: Add 1 and 7, which results in 8. 8 is bigger than 6, so ignore this sum and keep 1. Add 1 and 2. 1

假设我有一个数组:

int A[6] = {1, 7, 2, 4, 3, 9};
int array[5] = { 500, 1000, 500, 500, 400 };
我的目标号码是6。我们希望增加数字,直到达到目标或至少接近目标

排在第一位的数字具有更高的优先级,因此看起来如下所示:

Add 1 and 7, which results in 8. 
8 is bigger than 6, so ignore this sum and  keep 1. 
Add 1 and 2. 
1 plus 2 results in 3, which is smaller than 6, so we go to the next element after 2, 
     while holding the successful result.
Right now, result is 3, and the next element is 4, but 3 plus 4 is bigger than 6, 
     so we don't do anything with our result, and go to the next element. 
Result is still 3. 
3 plus 3 is 6, so we succesfully reached our target. 
We don't need to check the last element (9). 
In the end, we added 1, 2 and 3.
Part 1: 1500
0, 1
Part 2: 1400
2, 3, 4
另一个例子:

int B[4] = {5, 4, 3, 2};
程序:

Add 5 and 4. 
Result is bigger than 6, so ignore the sum and keep 5. 
Add 5 and 3. Still bigger than 6, ignore the sum and keep 5. 
5 plus 2, 7. Bigger than 6 again. 
The final result is 5. 
In the end we didn't add anything, and kept 5.
我似乎无法理解这种模式的代码

编辑:我想出了一个有效的代码。此外,它会一直执行,直到数组中没有剩余的元素,将数组划分为多个部分,然后重新执行该过程,直到数组中没有剩余的元素需要处理为止。这是:

int array[5] = { 500, 1000, 500, 500, 400 };
int arraySize = sizeof(array) / sizeof(array[0]);

int i = 1;
int j = 0;
int sum;
while (j < arraySize)
{
    sum = 0;
    sum += array[j];

    while (i < arraySize)
    {
        if (sum + array[i] <= 1500)
        {
            sum += array[i];
            array[i] = 0;
            i++;
        }
        else
            i++;
    }
    if (sum != 0)
        cout << "Part " << j + 1 << ": " << sum << endl;
    j++;
    i = j + 1;
}
代码返回

Part 1: 1500
Part 2: 1400
它执行与我给出的其他两个示例相同的过程,但使用所有元素重新执行:

Add 500 and 1000, which results in 1500.
1500 is the target number, so doing a sum is not needed anymore, 
so it prints the first part that fit in the target, 
while maintaining the priority protocol.
Now, add 500 (the 3rd element) plus 500, which results in 1000.
1000 is smaller than 1500, so we keep going.
1000 plus 400 is 1400, which is smaller than 1500, but is the last
element, so we finish here.
嗯,代码是有效的,但它是一种“尝试和错误”的成功。另外,我还有一个新问题,那就是:获取求和成功的元素的索引。它应该打印如下内容:

Add 1 and 7, which results in 8. 
8 is bigger than 6, so ignore this sum and  keep 1. 
Add 1 and 2. 
1 plus 2 results in 3, which is smaller than 6, so we go to the next element after 2, 
     while holding the successful result.
Right now, result is 3, and the next element is 4, but 3 plus 4 is bigger than 6, 
     so we don't do anything with our result, and go to the next element. 
Result is still 3. 
3 plus 3 is 6, so we succesfully reached our target. 
We don't need to check the last element (9). 
In the end, we added 1, 2 and 3.
Part 1: 1500
0, 1
Part 2: 1400
2, 3, 4

问题似乎相当简单。你试过自己解决吗?如果是这样,请展示你的尝试,因为这有助于了解你的困境。如果你展示你的尝试,发布代码,说出你期望的,没有发生的,等等,这会很有帮助。你可以从用英语描述解决方案开始,用足够具体的措辞,让其他人按照你说的去做,从而得出答案。那么用代码编写应该很容易。您好,谢谢您对我的问题感兴趣。我用一些新信息编辑了我的问题。谢谢编辑是一大进步。但是,您不应该将其作为一种解决方案,而是一种尝试,问题可以集中在“如何获得与总和相对应的索引?”