题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
好麻烦啊这个
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int[] a = new int[6]; int[] m = new int[4]; int goods = 0; int change = 0; int balance = 0; String[] res = new String[] { "E002:Denomination error", "S001:Initialization is successful", "S002:Pay success,balance=", "S003:Buy success,balance=", "E003:Change is not enough, pay fail", "E005:All the goods sold out", "E006:Goods does not exist", "E007:The goods sold out", "E008:Lack of balance", "E009:Work failure", "E010:Parameter error" }; while (in.hasNextLine()) { String line = in.nextLine(); String[] cmds = line.split(";"); for (String cmd : cmds) { if (cmd.equals("") || cmd.equals("\n")) { continue; } String[] parts = cmd.split(" "); switch (parts[0]) { case "r": String[] r1 = parts[1].split("-"); for (int i = 0; i < 6; i++) { a[i] = Integer.parseInt(r1[i]); goods += a[i]; } String[] r2 = parts[2].split("-"); m[0] = Integer.parseInt(r2[0]); m[1] = Integer.parseInt(r2[1]); m[2] = Integer.parseInt(r2[2]); m[3] = Integer.parseInt(r2[3]); change += m[0] + m[1] * 2; System.out.println(res[1]); break; case "p": int p1 = Integer.parseInt(parts[1]); if (p1 != 1 && p1 != 2 && p1 != 5 && p1 != 10) { System.out.println(res[0]); } else if (p1 >= 5 && change < p1) { System.out.println(res[4]); } else if (goods == 0) { System.out.println(res[5]); } else { switch (p1) { case 1: m[0]++; change++; balance++; break; case 2: m[1]++; change += 2; balance += 2; break; case 5: m[2]++; balance += 5; break; case 10: m[3]++; balance += 10; } System.out.println(res[2] + balance); } break; case "b": int b1 = 0; int b2 = 0; switch (parts[1]) { case "A1": b1 = 0; b2 = 2; break; case "A2": b1 = 1; b2 = 3; break; case "A3": b1 = 2; b2 = 4; break; case "A4": b1 = 3; b2 = 5; break; case "A5": b1 = 4; b2 = 8; break; case "A6": b1 = 5; b2 = 6; } if (b2 == 0) { System.out.println(res[6]); } else if (a[b1] == 0) { System.out.println(res[7]); continue; } else if (balance < b2) { System.out.println(res[8]); } else { a[b1]--; balance -= b2; System.out.println(res[3] + balance); } break; case "c": if (balance == 0) { System.out.println(res[9]); } else { int c10 = Math.min(m[3], balance / 10); balance -= c10 * 10; int c5 = Math.min(m[2], balance / 5); balance -= c5 * 5; int c2 = Math.min(m[1], balance / 2); balance -= c2 * 2; change -= c2 * 2; int c1 = Math.min(m[0], balance); balance -= c1; change -= c1; System.out.println("1 yuan coin number=" + c1); System.out.println("2 yuan coin number=" + c2); System.out.println("5 yuan coin number=" + c5); System.out.println("10 yuan coin number=" + c10); } break; case "q": if (parts[1].equals("0")) { System.out.println("A1 2 " + a[0]); System.out.println("A2 3 " + a[1]); System.out.println("A3 4 " + a[2]); System.out.println("A4 5 " + a[3]); System.out.println("A5 8 " + a[4]); System.out.println("A6 6 " + a[5]); } else if (parts[1].equals("1")) { System.out.println("1 yuan coin number=" + m[0]); System.out.println("2 yuan coin number=" + m[1]); System.out.println("5 yuan coin number=" + m[2]); System.out.println("10 yuan coin number=" + m[3]); } else { System.out.println(res[10]); } break; default: System.out.println(res[10]); } } } } }