The JavaScript implementation selects and equals the fixed value the N number _javascript technique from the array

Source: Internet
Author: User

Real-life problems may be abstracted into such a data model:

Select a few numbers from an array and let the numbers add up to the specified value.

Most readers should have the experience of online shopping, online shopping will generally have a single function, if the reader bought 70 yuan of merchandise, but must be full of 100 yuan to package mail, then the system will automatically recommend some products, add up to almost 100 dollars.

How does the system determine which products to recommend? This is actually just the model mentioned, we can put the price of hot items in an array, and then use the algorithm to find out what the price of the array and 30 yuan.

Less nonsense, small dishes to share a JavaScript version of the algorithm implementation.

Algorithm code:

function Getcombbysum (array,sum,tolerance,targetcount) {var util = {/* get combination from array arr:target array num: Combination item length Return:one array that contain combination arrays/getcombination:function (arr, num) {var r=[
]; (function f (t,a,n) {if (n==0) {return R.push (t);} for (Var i=0,l=a.length; i<=l-n; i++) {f (T.concat (A[i)), A.slice (
i+1), n-1);
}) ([],arr,num);
return R; },//take array index to an array getarrayindex:function (array) {var i = 0, r = []; for (i = 0;i<array.length;i++) {R.P
Ush (i);
} return R; }},logic = {//sort The array,then get what ' we need init:function (array,sum) {//clone array var _array = Array.concat
(), r = [], i = 0;
Sort by ASC _array.sort (function (a,b) {return a-b;}); Get all # when it's less than or equal sum for (i = 0;i<_array.length;i++) {if (_array[i]<=sum) {R.push (_array[
I]);

}else{break;}
return R; },//important function Core:function (array,sum,arrayindex,count,r) {var i = 0, k = 0, CombArray = [], _sum = 0, _cca = [], _cache = [];
if (count = = _returnmark) {return;}//get the current count combination Combarray = Util.getcombination (Arrayindex,count); for (i = 0;i<combarray.length;i++) {_cca = Combarray[i], _sum = 0; _cache = [];//calculate the sum from combination for (k = 0;k<_cca.length;k++)
{_sum + = array[_cca[k]]; _cache.push (Array[_cca[k]);} 
if (Math.Abs (_sum-sum) <= _tolerance) {R.push (_cache);}
} logic.core (Array,sum,arrayindex,count-1,r);

}, r = [], _array = [], _targetcount = 0, _tolerance = 0, _returnmark = 0; Check data _targetcount = Targetcount | |
_targetcount; _tolerance = Tolerance | |

_tolerance;
_array = Logic.init (array,sum);

if (_targetcount) {_returnmark = _targetcount-1;}

Logic.core (_array,sum,util.getarrayindex (_array), (_targetcount | | _array.length), r);
return R; }

Call Description:

Array: Arrays of data sources. Must be selected.

Sum: The sum of the sums. Must be selected.

Tolerance: tolerance. If you do not specify this parameter, the sum must be equal to the summation argument, specifying this parameter to allow the result to float within the tolerance range. Optional.

Targetcount: Number of operands. If you do not specify this parameter, the result contains all possible scenarios, specifying that this parameter can filter out a fixed number of numbers added, and if 3 is specified, the result contains only three numbers added. Optional.

Return value: Returns the array arrays of arrays, the elements in the inner array are operands, and the elements in the outer array are all possible results.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.