1. 程式人生 > >FCC----------- Exact Change

FCC----------- Exact Change

設計一個收銀程式 checkCashRegister() ,其把購買價格(price)作為第一個引數 , 付款金額

(cash)作為第二個引數, 和收銀機中零錢 (cid) 作為第三個引數. cid 是一個二維陣列,存著當前可用的找零.

當收銀機中的錢不夠找零時返回字串 “Insufficient Funds”. 如果正好則返回字串 “Closed”.
否則,返回應找回的零錢列表,且由大到小存在二維陣列中.

這是一些對你有幫助的資源:

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", 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".

程式碼如下:

function checkCashRegister(price, cash, cid) {
    //先把小數轉化為整數 按照分為單位轉化
    cash = cash * 100;
    price = price * 100
; //計算應該找的錢 var change = cash - price, changeLeft = change; //獲取錢櫃中總的錢 var totalCid = getTotalCid(cid); var result = []; //如果要找的錢多於錢櫃中的零錢返回 Insufficient Funds if (change > totalCid) { return 'Insufficient Funds'; } else if (change === totalCid) {//如果要找的錢剛好等於錢櫃中的錢返回 Closed return 'Closed'; } //取出錢的名稱 錢的總數和個數,並按照先找大錢的規則找零 ,把找的零錢存入一個數組中 for (var i = cid.length - 1; i >= 0; i--) { var coinName = cid[i][0], coinTotal = cid[i][1] * 100, coinValue = getValue(coinName), coinAmount = coinTotal / coinValue, toReturn = 0; while (changeLeft >= coinValue && coinAmount > 0) { changeLeft -= coinValue; coinAmount--; toReturn++; console.log(changeLeft); } if (toReturn > 0) { result.push([coinName, toReturn * (coinValue / 100)]); } } //判斷所有的零錢夠不夠給 if (getTotalCid(result) != change) { return 'Insufficient Funds'; } return result; //返回零錢陣列中的所有零錢 function getTotalCid(cid) { var total = 0; for (var i = 0; i < cid.length; i++) { total += cid[i][1] * 100; } return total; } function getValue(coinOrBill) { switch (coinOrBill) { case 'PENNY': return 1; case 'NICKEL': return 5; case 'DIME': return 10; case 'QUARTER': return 25; case 'ONE': return 100; case 'FIVE': return 500; case 'TEN': return 1000; case 'TWENTY': return 2000; case 'ONE HUNDRED': return 10000; } } } // Example cash-in-drawer array: // [["PENNY", 1.01], 一分錢 101張 // ["NICKEL", 2.05], 五分錢 41張 // ["DIME", 3.10], 一毛錢 31張 // ["QUARTER", 4.25], 2毛5分 17張 // ["ONE", 90.00], 一塊錢 90張 // ["FIVE", 55.00], 五塊錢 11張 // ["TEN", 20.00], 十塊錢 2張 // ["TWENTY", 60.00], 二十塊 3張 // ["ONE HUNDRED", 100.00]] 一百塊 1張 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]]);

相關推薦

FCC----------- Exact Change

設計一個收銀程式 checkCashRegister() ,其把購買價格(price)作為第一個引數 , 付款金額 (cash)作為第二個引數, 和收銀機中零錢 (cid) 作為第三個引數. cid 是一個二維陣列,存著當前可用的找零.

FCC算法題--Exact Change

global 記得 可用 one web 設計 ons switch org 題目: 設計一個收銀程序 checkCashRegister() ,其把購買價格(price)作為第一個參數 , 付款金額 (cash)作為第二個參數, 和收銀機中零錢 (cid) 作為第三個參數

Exact ChangeFCC)

思路見註釋: 設計一個收銀程式 checkCashRegister() ,其把購買價格(price)作為第一個引數 , 付款金額 (cash)作為第二個引數, 和收銀機中零錢 (cid) 作為第三個引數. cid 是一個二維陣列,存著當前可用的找零. 當收銀機中的錢

Exact Change(01背包)

BE sync accept AS ace ont size always edi 描述 Seller: That will be fourteen dollars. Buyer: Here‘s a twenty. Seller: Sorry, I don‘t have

Exact Change

題目 設計一個收銀程式 checkCashRegister() ,其把購買價格(price)作為第一個引數 , 付款金額 (cash)作為第二個引數, 和收銀機中零錢 (cid) 作為第三個引數. cid 是一個二維陣列,存著當前可用的找零. 當收銀機中的

POJ 2581 Exact Change Only(dp)

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned.  "Not in Vegas, I

poj 2581 Exact Change Only (dp)

Boudreaux reached over and shook awake Thibodeaux, who had dozed off somewhere in New Mexico. "Where we at?" Thibodeaux groggily yawned. "Not in Vegas, I g

Exact Change Only

題目:給你幾種不同面值的貨幣已知數量,求能否組成一個金額,如果有多組輸出總數最小的。 分析:dp,揹包。首先將小數轉化成整數(乘100),然後每個錢幣看成一個物品01揹包即可。              用一個數組記錄每種錢幣的使用情況,以及使用的總金錢數。 說明:面值從大

Cocos2d-x 更改文字換行風格 ( cocos2dx change line )

pad 限制長度 != inline detail _id 興趣研究 end sans Cocos2dx change line在 cocos2dx change line 的實現中,我們能夠簡單的使用 dimensions屬性控制換行。使用它僅僅需將相應的參數值傳入構造

MySQL使用pt-online-change-schema工具在線修改1.6億級數據表結構

影響 comment 失敗 tle 當前 www 表結構 oca 鎖表 摘 要:本文闡述了MySQL DDL 的問題現狀、pt-online-schema-change的工作原理,並實際利用pt-online-schema-change工具在線修改生產環境下1.6億級數據

java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representable decimal result異常的解決方法

add syn ati contain 無限循環小數 報錯 ref 情況 sca JAVA程序異常:java.lang.ArithmeticException: Non-terminating decimal expansion; no exact representabl

pt-osc全解pt-online-schema-change

pt-osc MySQL 大字段的DDL操作:加減字段、索引、修改字段屬性等,在5.1之前都是非常耗時耗力的,特別是會對MySQL服務產生影響。在5.1之後隨著Plugin Innodb的出現在線加索引的提高了很多,但是還會影響(時間縮短了),主要是出現了MDL鎖。不過5.6可以避免上面的情況,但目前

多個jdk 變更 引起 tomcat插件 啟動不了 The JRE could not be found.Edit the server and change the JRE location.

變更 runtime win jdk nts nvi bsp 選擇 ould The JRE could not be found.Edit the server and change the JRE location. 在Windows->Preference

Packet for query is too large (1166 > 1024). You can change this value

win you 修改配置 服務 grep can hang sql 目前 轉載: MySQL max_allowed_packet 設置過小導致記錄寫入失敗 mysql根據配置文件會限制server接受的數據包大小。 有時候大的插入和更新會受max_allowed_p

Coin Change

add .com range wrap str min sent sin tput https://github.com/Premiumlab/Python-for-Algorithms--Data-Structures--and-Interviews/blob/maste

fcc——有關循環的問題

mage oop person ould loop ide fin png you You can’t say that you didn’t find a person until you have searehd through the entire co

work picture change

tran ble tle borde ans position pict erp enter first: <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8"&

To change for better

技術分享 change cnblogs .cn png ges mage images com To change for better

radio change事件

field tex true 事件 func 沒有 text 適用於 方法 change事件 <input type="radio" name="rr" value=“1” /> <input type="radio" name="rr" value="2

[leetcode-322-Coin Change]

當前 compute col any nom for .cn program tar You are given coins of different denominations and a total amount of money amount. Write a fun