我有一个数组,其中包含多个数字。我希望该函数从另一个数组中返回最多3个数字的数组,这些数组加在一起后等于另一个数字。
它也可以是1或2个数字,但第一个数字必须尽可能大。
例如:
var array = [1,2,3,4,5,6,7,8,9,10];
var number: 25;
所需输出:[10,10,5]
或
var array = [1,2,3,4,5,6,7,8,9,10];
var number: 11;
所需输出:[10,1]
或
var array = [1,2,3,4,5,6,7,8,9,10];
var number: 5;
期望的输出:[5]
您可能会看到,我正在构建一个小的飞镖应用程序来计算您的分数。这是最后一部分,我需要显示哪种飞镖组合可以使您精确达到0,而无论我扔了多少支飞镖。
我可以遍历我的数字并将它们放在一个数组中,但是一旦组合需要在另一个数字之前有一个较小的数字,它就无法计算。这是代码:
// the numbers to add
var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
// The number
var output = 167;
// The variable for the end result
var dartsToFinishArr = [];
// Call my function
checkArray();
function checkArray() {
for (var j = 0; j < finishingNumbers.length; j++) {
var dartsToFinishArrTotal = 0;
if (dartsToFinishArr.length > 0) {
for (n = 0; n < dartsToFinishArr.length; n++) {
dartsToFinishArrTotal += dartsToFinishArr[n];
}
}
if (output <= 180) {
if (finishingNumbers[j] + dartsToFinishArrTotal == +($(".matchContainer .matchPlayerContainer .player.playerTurn .playerScore .value").text()) && dartsToFinishArr.length < 3) {
dartsToFinishArr.push(finishingNumbers[j]);
return;
} else if (finishingNumbers[j] + dartsToFinishArrTotal < output && dartsToFinishArr.length < 3) {
dartsToFinishArr.push(finishingNumbers[j]);
checkArray();
return;
} else if (dartsToFinishArrTotal != output) {
return;
}
}
}
}
这应该给我:
var dartsToFinishArr = [60, 57, 50]
但不是
答案 0 :(得分:1)
下面的逻辑遍历所有组合,直到找到三个满足您总数的数字。如果它们满足总数,也可以将少于三个的数字捷径化。
// the numbers to add
var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
// The number
var output = 167;
console.log( findValues( finishingNumbers, output ) );
function findValues ( numbers, desiredTotal ) {
numbers = numbers.slice(0).sort((a, b)=>b-a) //sort descending
for ( let i = 0; i < numbers.length; i++ ) {
if ( numbers[ i ] === desiredTotal ) return [ numbers[ i ] ];
// add in the numbers after the previous number at `i`
for ( let j = i + i; j < numbers.length; j++ ) {
if ( numbers[ i ] + numbers[ j ] === desiredTotal ) return [ numbers[ i ], numbers[ j ] ];
// add in the numbers after the previous number at `j`
for ( let k = j + 1; k < numbers.length; k++ ) {
if ( numbers[ i ] + numbers[ j ] + numbers[ k ] === desiredTotal ) return [ numbers[ i ], numbers[ j ], numbers[ k ] ];
}
}
}
}
答案 1 :(得分:0)
嗨。 我试图解决它,并且我的代码运行正常。
我还没有尝试过Javascript编程,但是我拥有C ++中的解决方案代码,因此您可以检查算法:
int numbersList[10] = {1,2,3,4,5,6,7,8,9,10};
int targetNumber;
cout<<"Enter target number: ";
cin>>targetNumber;
int a=numbersList[9], b=numbersList[9], c=numbersList[9];
if (a+b+c==targetNumber)
{
cout<<"Answer: a="<<a<<" b="<<b<<" c="<<c<<endl;
} else {
int aPos=9, bPos=9, cPos=9;
for(int i=0 ; i<719 ; i++)
{
a = numbersList[aPos];
b = numbersList[bPos];
c = numbersList[cPos];
if (a+b+c==targetNumber)
{
cout<<"Possible! a="<<a<<" b="<<b<<" c="<<c<<endl;
//break;
} else if (a+b+c>targetNumber)
{
if (cPos==0)
{
if (bPos==0)
{
if (aPos==0)
{
cout<<"Not possible!"<<endl;
break;
} else {
aPos -=1;
}
} else {
bPos -=1;
}
} else {
cPos -=1;
}
}
}
}
如果要检查程序,可以在这里尝试-> Click me
然后粘贴此代码:
/******************************************************************************
Online C++ Compiler.
Code, Compile, Run and Debug C++ program online.
Write your code in this editor and press "Run" button to compile and execute it.
*******************************************************************************/
#include <iostream>
using namespace std;
int main()
{
int numbersList[10] = {1,2,3,4,5,6,7,8,9,10};
int targetNumber;
cout<<"Enter target number: ";
cin>>targetNumber;
int a=numbersList[9], b=numbersList[9], c=numbersList[9];
if (a+b+c==targetNumber)
{
cout<<"Answer: a="<<a<<" b="<<b<<" c="<<c<<endl;
} else {
int aPos=9, bPos=9, cPos=9;
for(int i=0 ; i<719 ; i++)
{
a = numbersList[aPos];
b = numbersList[bPos];
c = numbersList[cPos];
if (a+b+c==targetNumber)
{
cout<<"Possible! a="<<a<<" b="<<b<<" c="<<c<<endl;
//break;
} else if (a+b+c>targetNumber)
{
if (cPos==0)
{
if (bPos==0)
{
if (aPos==0)
{
cout<<"Not possible!"<<endl;
break;
} else {
aPos -=1;
}
} else {
bPos -=1;
}
} else {
cPos -=1;
}
}
}
}
return 0;
}
希望您能得到答案:)
答案 2 :(得分:0)
这是我尝试用javascript解决您的问题的方法:
// the numbers to add
var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
// The number
var output = 167;
// The variable for the end result
var dartsToFinishArr = [];
for (let i = 0; i < finishingNumbers.length; i++) {
const element1 = finishingNumbers[i];
if (element1 === output) {
dartsToFinishArr = [element1];
return;
}
for (let j = 0; j < finishingNumbers.length; j++) {
const element2 = finishingNumbers[j];
if (element1 + element2 == output) {
dartsToFinishArr = [element1, element2];
return;
}
for (let k = 0; k < finishingNumbers.length; k++) {
const element3 = finishingNumbers[k];
if (element1 + element2 + element3 === output) {
dartsToFinishArr = [element1, element2, element3];
return;
}
}
}
}
这似乎是"how to debug a small program"的相当典型的情况,我强烈建议您阅读整篇博文,因为它将大大提高您的编程技能。
现在我们有了一个可行的解决方案,让我们一起玩吧!此解决方案满足您的要求,但是您注意到我有3个嵌套的for循环。这不是很糟糕,但是如果您的要求要更改为在解决方案中包含任意个数字,那么我的代码很快就会一团糟地复制粘贴。我们是否可以构建一个解决方案,该解决方案吸收解决方案中的数量并始终能够进行计算?你打赌!对于recursion,这是一个完美的方案。
这是一个更复杂但更通用的解决方案:
// the numbers to add
var finishingNumbers = [60, 57, 54, 51, 50, 48, 45, 42, 40, 39, 38, 36, 34, 33, 32, 30, 28, 27, 26, 25, 24, 22, 21, 20, 19, 18, 17, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1];
// The number
var output = 167;
// The variable for the end result
var dartsToFinishArr = [];
// The maximum amount of numbers allowed in the solution;
const maxNumbers = 3;
function sum(total, element) {
return total + element;
}
function recursiveSolution(possibleSolution) {
for (let element of finishingNumbers) {
// Special case when we haven't added anything to the solution yet
var total = element;
if (possibleSolution.length > 0) {
total = possibleSolution.reduce(sum) + element;
}
if (total === output) {
dartsToFinishArr = possibleSolution;
dartsToFinishArr.push(element);
//Solution found, return recursively.
return true;
}
if (total < output && possibleSolution.length < maxNumbers - 1) {
//There's room for more numbers, continue recursively
const newPossibleSolution = possibleSolution.slice(0);
newPossibleSolution.push(element);
if (recursiveSolution(newPossibleSolution)) {
//The recursion worked, return recursively.
return true;
}
}
}
//We tried everything with the current possible solution. Let's backtrack one number and try the next one.
return false;
}
// Start the recursion with an empty solution
recursiveSolution([]);
console.debug(dartsToFinishArr);
现在您可以修改maxNumbers,并且可以获得所需的任何解决方案!