JavaScript realizes the composition and arrangement of coin denomination

Source: Internet
Author: User
Tags sort

Problem Description:

In England the currency are made up of pound,, and Pence, p, and there are eight coins at general circulation:

1p, 2p, 5p, 10p, 20p, 50p, 1 (100p) and 2 (200p).

It is possible to make 2 in the following way:

One + 150p + 220p + 15p + 12p + 31p

How many different ways can 2 is made using any number of coins?

Realize:

Ideas:

1. Resolve 1 points, 2 points, 5 points first

2. Recursive ideas to solve other situations

(function () {//Prepare Array.prototype.addRange = function (arr) {if (arr = = undefined) {return new Array ();  
var ret = new Array ();  
for (var i = 0; i < this.length i++) {Ret.push (this[i));  
for (var i = 0; i < arr.length i++) {Ret.push (arr[i));  
return ret;  
      
} ARRAY.PROTOTYPE.CONSTAINSARR = function (arr) {var arr1 = new Array ();  
var iscontains = true;  
      
for (var i = 0;i < this.length; i++) Arr1.push (this[i));  
var arr2 = new Array ();  
      
for (var i = 0;i < arr.length; i++) Arr2.push (arr[i));  
var iscontain = true;  
      
for (var i = 0;i < Arr1.length; i++) {Iscontain = true;  
Arr1[i] = Arr1[i].sort (function (a,b) {return a-b;});  
      
ARR2 = Arr2.sort (function (a,b) {return a-b;});  
for (var j = 0; j< arr1[i].length + +) {if (arr1[i][j]!=arr2[j)) Iscontain = false;  
} if (Iscontain) return true;  
return false;  
      
      
      
      }
      
Logic//View this column more highlights: http://www.bianceng.cnhttp://www.bianceng.cn/webkf/script/var coins = new Array (1, 2,  
      
      
5, 10, 20, 50, 100, 200);  
      
var sln = function Solve (n) {if (Coins.indexof (n) = = 1) throw ex ("Unexpected Coin");  
if (n== 1) return the new Array ([1]);  
if (n = = 2) return to New Array ([1,1],[2]);  
      
if (n = = 5) return to New Array ([1,1,1,1,1],[1,1,1,2],[1,2,2],[5]);  
if (n = =) {var arr1 = solve (5);  
var combined= new Array (); for (var i = 0, I < arr1.length;i++) {for (var j = 0;j < arr1.length;j++) {var ret = Arr1[i].addrange (arr1[  
      
J]);  
      
if (Combined.length = = 0 | |!combined.constainsarr (ret)) {Combined.push (ret);}  
} return combined;  
} if (n = =) {var arr1 = solve (10);  
Console.log ("solved 10");  
      
Console.log (ARR1);  
var combined= new Array (); for (var i = 0, I < arr1.length;i++) {for (var j = 0;j < arr1.length;j++) {var ret = Arr1[i].addrange (arr1[ J]); 
if (Combined.length = = 0 | |!combined.constainsarr (ret)) {Combined.push (ret);}  
} return combined;  
} if (n = =) {var arr1 = solve (20);  
      
var arr2 = solve (10);  
var combined= new Array (); for (var i = 0, I < arr1.length;i++) {for (var j = 0;j < arr1.length;j++) {var ret = Arr1[i].addrange (arr1[  
J]);  
      
      
if (Combined.length = = 0 | |!combined.constainsarr (ret)) {Combined.push (ret);}  
} var combined2 = new Array (); for (var i = 0, I < arr2.length;i++) {for (var j = 0;j < combined.length;j++) {var ret = Arr2[i].addrange (c  
OMBINED[J]);  
      
if (Combined2.length = = 0 | |!combined2.constainsarr (ret)) {Combined2.push (ret);}  
      
} return COMBINED2;  
      
} if (n==) {var arr1 = solve (50);  
var combined= new Array (); for (var i = 0, I < arr1.length;i++) {for (var j = 0;j < arr1.length;j++) {var ret = Arr1[i].addrange (arr1[  
J]); if (combined.length = = 0 | |  
      
!combined.constainsarr (ret)) {Combined.push (ret);}  
} return combined;  
      
} if (n==) {var arr1 = solve (100);  
var combined= new Array (); for (var i = 0, I < arr1.length;i++) {for (var j = 0;j < arr1.length;j++) {var ret = Arr1[i].addrange (arr1[  
J]);  
      
if (Combined.length = = 0 | |!combined.constainsarr (ret)) {Combined.push (ret);}  
} return combined;  
} var ret = SLN (50);  
      
for (var i =0;i < ret.length i++) Console.log (ret[i)); })();
Related Article

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.