c - 在数组中查找总和最接近或等于给定数字的 5 个元素

标签 c arrays algorithm

我需要一些提示来找到一种算法,该算法返回总和最接近或等于给定数字的 5 元素。

这些元素是大于 0 的数字,并且在尝试获取给定数字时每次只能“使用” 1 次。

假设我们有一个数组 {1, 2, 3, 4, 5, 6, 7, 8, 9, 10} 和我们想要得到的数字 21 。它应该返回 {2, 3, 4, 5, 7}

非常感谢任何帮助!

最佳答案

ok n loops all begin with counts = 0 (in this case n = 5 )
all loops end at MainArraySize (in this case MainArraySize = 10)

int RequiredSum = ValueOfRequiredSum;
int CurrentSum = 0;
int CurrentClosestSum = 0;
int[] Finalindexesrequired = int[5]{0,0,0,0,0};

//U might want to add Duplicates 
duplicate_count ++;
int[5][] FinalindexesRequiredDuplicates= new int[5][];


for(int loopcount1 = 0, loopcount1++, loopcount < MainArraySize-1)
{
for(int loopcount2 = 0, loopcount2++, loopcount < MainArraySize-1)
{..
..
..
for(int loopcount5 = 0, loopcount5++, loopcount < MainArraySize-1)
{

-------------------------------------
Now this is all inside the 5th loop
//Process logic here
//Looping for first time
if(CurrentSum = 0)
{Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]
CurrentClosestSum = CurrentSum
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;
}

Currentsum = MainArray[loopcount1] + MainArray[loopcount2] + .... + MainArray[loopcount5]


if((RequiredSum - CurrentSum) < (RequiredSum - CurrentClosestSum))
{
//Am gonna change the indexes because the currentsum ITERATION is closer
FinalindexesRequired[0] = loopcount1;
FinalindexesRequired[1] = loopcount2;
..
..
FinalindexesRequired[4] = loopcount2;

//If u wanted the duplicates also, since u came to a fresher ITERATION
Reset the duplicatecount to 0 and remove all duplicates because they aint valid anymore
}

//What u Might want to Add is this
if((Requiredsum - CurrentSum) = (RequiredSum - CurrentCosestSum))
{
//Hey we got Duplicates
duplicate_count ++;
FinalindexesRequiredDuplicates[0][duplicate_count] = loopcount1;
FinalindexesRequiredDuplicates[1][duplicate_count] = loopcount1;
..
..
FinalindexesRequiredDuplicates[5][duplicate_count] = loopcount1;
}

}



-------------------------------- End of 5th loop
}}}}}



//FINALLY AFTER EXITING I THINK U HAVE UR ANSWER IN

MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]


//IN CASE OF DUPLICATES U HAVE UR ANSWER IN
set 1:
MAINARRAY[FINALINDEXESREQUIRED[0]]
MAINARRAY[FINALINDEXESREQUIRED[1]]
MAINARRAY[FINALINDEXESREQUIRED[2]]
MAINARRAY[FINALINDEXESREQUIRED[3]]
MAINARRAY[FINALINDEXESREQUIRED[4]]

other sets:
MAINARRAY[FinalindexesRequiredDuplicates[0][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[1][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[2][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[3][0...n]]
MAINARRAY[FinalindexesRequiredDuplicates[4][0...n]]

关于c - 在数组中查找总和最接近或等于给定数字的 5 个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14309869/

相关文章:

PHP/MySQL 大数据量测试应用

javascript - 如何使用 Parse.com 中的云代码将UniqueObject 添加到非当前用户类?

c++ - 取消设置最右边的设置位

c++ - 将相同操作应用于所有派生对象的任何设计模式

c++ - 将结构拆分为函数参数列表的最佳方法

C源代码,将字符串中单词的首字母从小写更改为大写

在C中连接两个字符串

javascript - 计算javascript中的总值没有给出正确的结果

c++ - C++中定义一个行数未知的数组

c - select() 仅返回正值一次