FCC算法题--Exact Change
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了FCC算法题--Exact Change相关的知识,希望对你有一定的参考价值。
题目:
设计一个收银程序 checkCashRegister()
,其把购买价格(price
)作为第一个参数 , 付款金额 (cash
)作为第二个参数, 和收银机中零钱 (cid
) 作为第三个参数.
cid
是一个二维数组,存着当前可用的找零.
当收银机中的钱不够找零时返回字符串 "Insufficient Funds"
. 如果正好则返回字符串 "Closed"
.
否则, 返回应找回的零钱列表,且由大到小存在二维数组中.
当你遇到困难的时候,记得查看错误提示、阅读文档、搜索、提问。
这是一些对你有帮助的资源:
1 function checkCashRegister(price, cash, cid) { 2 var change; 3 var sumCid; 4 5 // Here is your change, ma‘am. 6 var obj = { 7 penny: cid[0][1], 8 nickel: cid[1][1], 9 dime: cid[2][1], 10 quarter: cid[3][1], 11 one: cid[4][1], 12 five: cid[5][1], 13 ten: cid[6][1], 14 twenty: cid[7][1], 15 oneHundred:cid[8][1] 16 }; 17 18 //购买价格(price), 付款金额 (cash), 收银机中零钱 (cid) 19 change = cash - price; 20 //console.log("typeof(change):", typeof(change)); //number 21 //console.log("change:", change); 22 23 sumCid = 24 obj.penny + 25 obj.nickel + 26 obj.dime + 27 obj.quarter + 28 obj.one + 29 obj.five + 30 obj.ten + 31 obj.twenty + 32 obj.oneHundred; 33 34 sumCid = Number(sumCid.toFixed(2)); //保留两位小数 35 36 //console.log(typeof(sumCid)); //number 37 //console.log("sumCid:", sumCid); 38 39 if (sumCid < change) { 40 //console.log("sumCid小于change!"); 41 return "Insufficient Funds"; //余额不足 42 } 43 else if (sumCid === change) { 44 //console.log("sumCid等于change!"); 45 return "Closed"; 46 } 47 else { 48 // 1.零钱不够,返回余额不足 49 // 2.零钱足够,找回零钱 50 //console.log("sumCid大于change!"); 51 overChange(change, cid, sumCid); 52 } 53 } 54 55 function overChange(change, arr, sumCid) { 56 var array = []; 57 var len = arr.length; 58 59 for (var i = len - 1; i >= 0; i--) { 60 if (arr[i][1] === 0) continue; 61 62 if (arr[i][1] < change) { 63 array.push(arr[i]); 64 change -= arr[i][1]; 65 change = Number(change.toFixed(2)); 66 //console.log("current change:", change); 67 } 68 69 else if (arr[i][1] > change) { 70 var x = arr[i][0]; // 取得零钱名称 71 //console.log("x:", x); 72 var y = 0; 73 74 switch(x) { 75 case "ONE HUNDRED": 76 y = Math.floor(change / 100) * 100; 77 break; 78 case "TWENTY": 79 y = Math.floor(change / 20) * 20; 80 break; 81 case "TEN": 82 y = Math.floor(change / 10) * 10; 83 break; 84 case "FIVE": 85 y = Math.floor(change / 5) * 5; 86 break; 87 case "ONE": 88 y = Math.floor(change / 1); 89 break; 90 case "QUARTER": 91 y = Math.floor(change / 0.25) * 0.25; 92 break; 93 case "DIME": 94 y = Math.floor(change / 0.10) * 0.10; 95 break; 96 case "NICKEL": 97 y = Math.floor(change / 0.05) * 0.05; 98 break; 99 case "PENNY": 100 y = Math.floor(change / 0.01) * 0.01; 101 break; 102 } 103 104 console.log(y); 105 console.log(sumCid - arr[i][1] < change); 106 107 if (y === 0 && sumCid - arr[i][1] < change) { 108 //console.log("Insufficient Funds"); 109 return "Insufficient Funds"; 110 } 111 112 else if (y === 0) { 113 continue; 114 } 115 116 else { 117 if (y < 1) { 118 y = y; 119 } 120 var array1 = []; 121 array1.push(arr[i][0]); 122 array1.push(y); 123 array.push(array1); 124 change -= y; 125 change = Number(change.toFixed(2)); 126 //console.log("current change:", change); 127 } 128 } 129 } 130 131 if (change === 0) { 132 //console.log("array:", array); 133 return array; 134 } 135 else { 136 //console.log("Insufficient Funds"); 137 return "Insufficient Funds"; 138 } 139 140 } 141 142 checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]]);
checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])
应该返回一个数组.checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
应该返回一个字符串.checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
应该返回一个字符串.checkCashRegister(19.50, 20.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])
应该返回 [["QUARTER", 0.50]]
.checkCashRegister(3.26, 100.00, [["PENNY", 1.01], ["NICKEL", 2.05], ["DIME", 3.10], ["QUARTER", 4.25], ["ONE", 90.00], ["FIVE", 55.00], ["TEN", 20.00], ["TWENTY", 60.00], ["ONE HUNDRED", 100.00]])
应该返回 [["TWENTY", 60.00], ["TEN", 20.00], ["FIVE", 15], ["ONE", 1], ["QUARTER", 0.50], ["DIME", 0.20], ["PENNY", 0.04]]
.checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
应该返回 "Insufficient Funds".checkCashRegister(19.50, 20.00, [["PENNY", 0.01], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 1.00], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
应该返回 "Insufficient Funds".checkCashRegister(19.50, 20.00, [["PENNY", 0.50], ["NICKEL", 0], ["DIME", 0], ["QUARTER", 0], ["ONE", 0], ["FIVE", 0], ["TEN", 0], ["TWENTY", 0], ["ONE HUNDRED", 0]])
应该返回 "Closed".---------------------------------------------------------------------------------------
以上是关于FCC算法题--Exact Change的主要内容,如果未能解决你的问题,请参考以下文章
FCC JS基础算法题:Chunky Monkey(分割数组)