题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

import java.util.*;

/*
	1.难度不难,有耐心慢慢代码实现就好
	2.读题也是一种能力,有几个坑描述没提到
	3.默认的投币会加到存钱盒,以及退币减掉存钱盒里的钱币
	4.好像直接实现商品和存钱盒类会更简洁易懂
*/
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 注意 hasNext 和 hasNextLine 的区别
        while (in.hasNext()) { // 注意 while 处理多个 case
            String[] ss = in.nextLine().split(";");
            for (int i = 0; i < ss.length; i++) {
                System.out.println(handle(ss[i]));
            }
            // System.out.println(handle("r 6-5-4-3-2-1 4-3-2-1"));
        }
    }

    public static String handle(String s) {
        String[] ss = s.split("\\s+");
        // 1、系统初始化 r
        if ("r".equals(ss[0])) {
            return rInit(s);
        }
        // 2、投币 p
        if ("p".equals(ss[0])) {
            return pCoin(s);
        }
        // 3、购买商品 b
        if ("b".equals(ss[0])) {
            return bBuy(s);
        }
        // 4、退币 c
        if ("c".equals(ss[0])) {
            return cRefund(s);
        }
        // 5、查询 q
        if ("q".equals(ss[0])) {
            return qQuery(s);
        }
        return "E010:Parameter error";
    }

    static HashMap<String, Integer> merchandise;
    static HashMap<String, Integer> currency;
    static HashMap<String, Integer> merchandisePrice = new HashMap<>();
    static {
        merchandisePrice.put("A1", 2);
        merchandisePrice.put("A2", 3);
        merchandisePrice.put("A3", 4);
        merchandisePrice.put("A4", 5);
        merchandisePrice.put("A5", 8);
        merchandisePrice.put("A6", 6);
    }

    public static String rInit(String s) {
        //TODO
        merchandise = new HashMap<>();
        currency = new HashMap<>();
        String[] ss1 = s.split(" ");
        String[] ss2 = ss1[1].split("-");
        String[] ss3 = ss1[2].split("-");
        merchandise.put("A1", Integer.parseInt(ss2[0]));
        merchandise.put("A2", Integer.parseInt(ss2[1]));
        merchandise.put("A3", Integer.parseInt(ss2[2]));
        merchandise.put("A4", Integer.parseInt(ss2[3]));
        merchandise.put("A5", Integer.parseInt(ss2[4]));
        merchandise.put("A6", Integer.parseInt(ss2[5]));

        currency.put("1", Integer.parseInt(ss3[0]));
        currency.put("2", Integer.parseInt(ss3[1]));
        currency.put("5", Integer.parseInt(ss3[2]));
        currency.put("10", Integer.parseInt(ss3[3]));

        return "S001:Initialization is successful";
    }

    static int balance = 0;

    public static String pCoin(String s) {
        //TODO
        String[] ss = s.split(" ");
        // 如果投入非1元、2元、5元、10元的钱币面额
        if (!"1".equals(ss[1]) && !"2".equals(ss[1]) && !"5".equals(ss[1]) &&
                !"10".equals(ss[1])) {
            return "E002:Denomination error";
        }
        // 如果存钱盒中1元和2元面额钱币总额小于本次投入的钱币面额
        if (Integer.parseInt(ss[1]) != 1 && Integer.parseInt(ss[1]) != 2 && (currency.get("1") + currency.get("2") * 2) < Integer.parseInt(ss[1])) {
            return "E003:Change is not enough, pay fail";
        }
        // 如果自动售货机中商品全部销售完毕
        int count = 0;
        for (String key : merchandise.keySet()) {
            count += merchandise.get(key);
        }
        if (count == 0) {
            return "E005:All the goods sold out";
        }
        // 如果投币成功
        balance += Integer.parseInt(ss[1]);
        currency.put(ss[1], currency.get(ss[1]) + 1);
        return "S002:Pay success,balance=" + balance;
    }

    public static String bBuy(String s) {
        //TODO
        String[] ss = s.split(" ");
        // 如果购买的商品不在商品列表中
        if (!merchandise.containsKey(ss[1])) {
            return "E006:Goods does not exist";
        }
        // 如果所购买的商品的数量为0
        if (merchandise.get(ss[1]) == 0) {
            return "E007:The goods sold out";
        }
        // 如果投币余额小于待购买商品价格
        if (balance < merchandisePrice.get(ss[1])) {
            return "E008:Lack of balance";
        }
        // 如果购买成功
        balance -= merchandisePrice.get(ss[1]);
        return "S003:Buy success,balance=" + balance;
    }

    public static String cRefund(String s) {
        //TODO
        // 如果投币余额等于0的情况下
        if (balance == 0) {
            return "E009:Work failure";
        }
        // 如果投币余额大于0的情况下,按照 退币原则 进行“找零”,输出退币信息;
        int re1 = 0, re2 = 0, re5 = 0, re10 = 0;
        if (balance >= 10) {
            if (currency.get("10") >= balance / 10) {
                re10 = balance / 10;
                balance %= 10;
            } else {
                re10 = currency.get("10");
                balance -= re10 * 10;
            }
            currency.put("10", currency.get("10") - re10);
        }
        if (balance >= 5) {
            if (currency.get("5") >= balance / 5) {
                re5 = balance / 5;
                balance %= 5;
            } else {
                re5 = currency.get("5");
                balance -= re5 * 5;
            }
            currency.put("5", currency.get("5") - re5);
        }
        if (balance >= 2) {
            if (currency.get("2") >= balance / 2) {
                re2 = balance / 2;
                balance %= 2;
            } else {
                re2 = currency.get("2");
                balance -= re2 * 2;
            }
            currency.put("2",currency.get("2") - re2);
        }
        if (balance >= 1) {
            if (currency.get("1") >= balance) {
                re1 = balance;
                balance = 0;
            } else {
                re1 = currency.get("1");
                balance -= re1;
            }
            currency.put("1",currency.get("1") - re1);
        }
        // balance = 0;
        return "1 yuan coin number=" + re1 + "\n2 yuan coin number=" + re2 +
               "\n5 yuan coin number=" + re5 + "\n10 yuan coin number=" + re10;
    }

    public static String qQuery(String s) {
        //TODO
        String[] ss = s.split(" ");
        if (ss[1] == "0") {
            String[] ssMerch = new String[6];
            int idx = 0;
            for(String key : merchandise.keySet()) {
                ssMerch[idx] = merchandise.get(key) + " " + key + " " + merchandisePrice.get(key);
                idx++;
            }
            Arrays.sort(ssMerch);
            StringBuilder sb = new StringBuilder();
            for(int i = 0; i < ssMerch.length; i++) {
                sb.append(ssMerch[i] + "\n");
            }
            return sb.toString();
        } else if (ss[1] == "1") {
            return "1 yuan coin number=" + currency.get("1") + "\n2 yuan coin number=" + currency.get("2") + "\n5 yuan coin number=" + currency.get("5") + "\n10 yuan coin number=" + currency.get("10");
        } else {
            return "E010:Parameter error";
        }
    }
}

全部评论

相关推荐

不愿透露姓名的神秘牛友
01-10 16:40
点赞 评论 收藏
分享
01-28 16:12
中南大学 Java
几年前还没有chatgpt的时候,刷题真的是很痛苦。刷不出来只能看题解,题解有几个问题:第一个是每次看的写题解的人都不一样,很难有一个统一的思路;第二个也是最重要的是,题解只提供了作者自己的思路,但是没有办法告诉你你的思路哪里错了。其实很少有错误的思路,我只是需要被引导到正确的思路上面去。所以传统题解学习起来非常困难,每次做不出来难受,找题解更难受。但是现在chatgpt能做很多!它可以这样帮助你&nbsp;-1.&nbsp;可以直接按照你喜欢的语言生成各种解法的题解和分析复杂度。2.&nbsp;把题和你写的代码都发给它,它可以告诉你&nbsp;你的思路到底哪里有问题。有时候我发现我和题解非常接近,只是有一点点🤏想错了。只要改这一点点就是最优解。信心倍增。3.&nbsp;如果遇到不懂的题解可以一行一行询问为什么要这样写,chatgpt不会嫌你烦。有时候我觉得自己的range写错了,其实那样写也没错,只是chat老师的题解有一点优化,这个它都会讲清楚。4.&nbsp;它可以帮你找可以用同类型解法来做的题。然后它可以保持解法思路不变,用一个思路爽刷一个类型的题。如果题目之间思路又有变化,它会告诉你只有哪里变了,其他的地方还是老思路。5.&nbsp;它也可以直接帮你总结模板,易错点。经过chat老师的指导,我最大的改变是敢刷题了。之前刷题需要先找某一个人写的算法题repo,然后跟着某一个人他的思路刷他给的几个题。如果想写别的题,套用思路失败了,没有他的题解,也不知道到底哪里错了;看别人的题解,思路又乱了。这个问题在二分查找和dp类型的题里面特别常见。但是现在有chat老师,他会针对我的代码告诉我我哪里想错了,应该怎么做;还按照我写代码的习惯帮我总结了一套属于我的刷题模板。每天写题全是正反馈!
牛客981:不刷才是爽
AI时代的工作 VS 传...
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务