题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf?tpId=37&tqId=21321&rp=1&ru=/exam/oj/ta&qru=/exam/oj/ta&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D2%26tpId%3D37%26type%3D37&difficulty=undefined&judgeStatus=undefined&tags=&title=
模拟题花时间慢慢写ok了
踩坑用例:
- S003:Buy success,balance和S002:Pay success,balance这两个投币和购买操作打印的balance都是 已投币的金额
- q1 和 q0 都是不合法的,只有 q 1 和 q 0 是合法的
- 退币是先退面额大的,参考贪心算法经典案例找零
import java.util.*;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static int[][] productPriceToNums;
private static Map<Integer, String> indexToName;
private static Map<String, Integer> nameToIndex;
private static Map<Integer, Integer> priceToIndex;
private static int[][] priceToNums;
private static int putPrice;
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] commendLine = in.nextLine().split(";");
for (String commend : commendLine) {
char first = commend.charAt(0);
switch (first) {
case 'r':
operateR(commend);
break;
case 'p':
operateP(commend);
break;
case 'b':
operateB(commend);
break;
case 'c':
operateC();
break;
case 'q':
operateQ(commend);
break;
}
}
}
private static void operateR(String commend) {
initData();
String[] all = commend.split(" ");
String[] count = all[1].split("-");
String[] price = all[2].split("-");
int indexCount = 0;
for (String s : count) {
productPriceToNums[indexCount][1] = Integer.parseInt(s);
indexCount++;
}
int indexPrice = 0;
for (String s : price) {
priceToNums[indexPrice][1] = Integer.parseInt(s);
indexPrice++;
}
System.out.println("S001:Initialization is successful");
}
private static void operateP(String commend) {
String s = commend.substring(2);
if (soldOut()) {
System.out.println("E005:All the goods sold out");
return;
}
if ("1".equals(s) || "2".equals(s) || "5".equals(s) || "10".equals(s)) {
int price = Integer.parseInt(s);
//受限制投币
if ("5".equals(s) || "10".equals(s)) {
if (price > calculate12Price()) {
System.out.println("E003:Change is not enough, pay fail");
return;
}
}
int index = priceToIndex.get(price);
priceToNums[index][1]++;
putPrice += price;
System.out.println("S002:Pay success,balance=" + putPrice);
} else {
System.out.println("E002:Denomination error");
}
}
//购买商品
private static void operateB(String commend) {
String s = commend.substring(2);
if (!nameToIndex.containsKey(s)) {
System.out.println("E006:Goods does not exist");
return;
}
int index = nameToIndex.get(s);
//数量售空
if (productPriceToNums[index][1] == 0) {
System.out.println("E007:The goods sold out");
return;
}
int money = productPriceToNums[index][0];
if (putPrice < money) {
System.out.println("E008:Lack of balance");
return;
}
putPrice = putPrice - money;
System.out.println("S003:Buy success,balance=" + putPrice);
}
private static void operateC() {
if (putPrice == 0) {
System.out.println("E009:Work failure");
return;
}
//退币以及退币操作信息
int hasPrice10 = priceToNums[3][1];
int needPrice10 = putPrice / 10;
int backPrice10 = Math.min(hasPrice10, needPrice10);
priceToNums[3][1] -= backPrice10;
putPrice -= backPrice10 * 10;
int hasPrice5 = priceToNums[2][1];
int needPrice5 = putPrice / 5;
int backPrice5 = Math.min(hasPrice5, needPrice5);
priceToNums[2][1] -= backPrice5;
putPrice -= backPrice5 * 5;
int hasPrice2 = priceToNums[1][1];
int needPrice2 = putPrice / 2;
int backPrice2 = Math.min(hasPrice2, needPrice2);
priceToNums[1][1] -= backPrice2;
putPrice -= backPrice2 * 2;
int hasPrice1 = priceToNums[0][1];
int needPrice1 = putPrice;
int backPrice1 = Math.min(hasPrice1, needPrice1);
priceToNums[0][1] -= backPrice1;
putPrice -= backPrice1;
System.out.println("1 yuan coin number=" + backPrice1);
System.out.println("2 yuan coin number=" + backPrice2);
System.out.println("5 yuan coin number=" + backPrice5);
System.out.println("10 yuan coin number=" + backPrice10);
}
private static void operateQ(String commend) {
String s = commend.substring(2).trim();
if ("0".equals(s)) {
formatPrintProduct();
} else if ("1".equals(s)) {
formatPrintCoin();
} else {
System.out.println("E010:Parameter error");
}
}
private static void initData() {
productPriceToNums = new int[6][2];
productPriceToNums[0][0] = 2;
productPriceToNums[1][0] = 3;
productPriceToNums[2][0] = 4;
productPriceToNums[3][0] = 5;
productPriceToNums[4][0] = 8;
productPriceToNums[5][0] = 6;
indexToName = new HashMap<>();
indexToName.put(0, "A1");
indexToName.put(1, "A2");
indexToName.put(2, "A3");
indexToName.put(3, "A4");
indexToName.put(4, "A5");
indexToName.put(5, "A6");
nameToIndex = new HashMap<>();
nameToIndex.put("A1", 0);
nameToIndex.put("A2", 1);
nameToIndex.put("A3", 2);
nameToIndex.put("A4", 3);
nameToIndex.put("A5", 4);
nameToIndex.put("A6", 5);
priceToIndex = new HashMap<>();
priceToIndex.put(1, 0);
priceToIndex.put(2, 1);
priceToIndex.put(5, 2);
priceToIndex.put(10, 3);
priceToNums = new int[4][2];
priceToNums[0][0] = 1;
priceToNums[1][0] = 2;
priceToNums[2][0] = 5;
priceToNums[3][0] = 10;
putPrice = 0;
}
private static boolean soldOut() {
return productPriceToNums[0][0] == 0
&& productPriceToNums[1][0] == 0
&& productPriceToNums[2][0] == 0
&& productPriceToNums[3][0] == 0
&& productPriceToNums[4][0] == 0
&& productPriceToNums[5][0] == 0;
}
private static int calculate12Price() {
return priceToNums[0][1] + priceToNums[1][1] * 2;
}
private static void formatPrintProduct() {
for (int i = 0; i < 6; i++) {
String s = indexToName.get(i) + " " + productPriceToNums[i][0] + " " + productPriceToNums[i][1];
System.out.println(s);
}
}
private static void formatPrintCoin() {
for (int i = 0; i < 4; i++) {
String s = priceToNums[i][0] + " yuan coin number=" + priceToNums[i][1];
System.out.println(s);
}
}
}