中级农民
- 积分
- 102
- 大米
- 颗
- 鳄梨
- 个
- 水井
- 尺
- 蓝莓
- 颗
- 萝卜
- 根
- 小米
- 粒
- 学分
- 个
- 注册时间
- 2013-4-21
- 最后登录
- 1970-1-1
|
本帖最后由 amzn_chou 于 2021-5-2 12:36 编辑
这是我的代码:
- public class PayChange {
- // This keeps track all the denominations the cashier has in the register.
- // We use a reverse ordered TreeMap because we always want to find the change
- // with larger denominations. For example, if we want to find $2.65, we'll first
- // check if there is any $100, then $20, then $10, $5 and $1, etc.
- // All the denominations are converted to cents. For example if we have 3 dollars
- // in the register, it would be stored as 100 -> 3.
- private final TreeMap<Integer, Integer> register;
- public PayChange(TreeMap<Integer, Integer> register) { assert Collections.reverseOrder().equals(register.comparator());
- this.register = register;
- }
- /**
- * When you buy something in grocery, the cashier needs to pay the change. Write a method simulating how the cashier
- * find all the changes. For example you paid $5 for something worth $2.35, the cashier needs to find 2 $1, 6 dimes
- * and 5 one cents if there are enough of these.
- */
- public boolean payChange(Map<Integer, Integer> paid, int price) {
- int totalPaid = 0;
- for (int denomination : paid.keySet()) {
- totalPaid += denomination * paid.get(denomination);
- }
- // Not enough to cover the price.
- if (totalPaid < price) {
- return false;
- }
- // First we need to check if there is a solution, w/o actually paying the change.
- // Maybe there isn't a solution.
- int change = totalPaid - price;
- Map<Integer, Integer> possibleChange = new HashMap<>();
- for (int denomination : register.keySet()) {
- int needed = change / denomination;
- // We also have to consider what we have from the customer, not just what we have in the register.
- int nReduced = Math.min(needed, register.get(denomination) + paid.getOrDefault(denomination, 0));
- if (nReduced > 0) {
- change -= nReduced * denomination;
- possibleChange.put(denomination, nReduced);
- }
- if (change == 0) {
- break;
- }
- }
- // This means we cannot find the proper combination.
- if (change > 0) {
- return false;
- }
- // Add back what the customer paid to the register.
- for (int denomination : paid.keySet()) {
- register.put(denomination, register.getOrDefault(denomination, 0) + paid.get(denomination));
- }
- // Actually remove denominations from the register.
- for (int denomination : possibleChange.keySet()) {
- int nRemaining = register.get(denomination) - possibleChange.get(denomination);
- if (nRemaining == 0) {
- register.remove(denomination);
- } else {
- register.put(denomination, nRemaining);
- }
- }
- return true;
- }
- @Override
- public String toString() {
- return register.toString();
- }
- public static void main(String[] args) {
- TreeMap<Integer, Integer> register = new TreeMap<>(Collections.reverseOrder());
- register.put(100, 5);
- register.put(25, 4);
- register.put(1, 20);
- PayChange payChange = new PayChange(register);
- Map<Integer, Integer> paid = new HashMap<>();
- paid.put(500, 1);
- System.out.println(payChange.payChange(paid, 235));
- System.out.println(payChange);
- }
- }
复制代码
|
|