题解 | #自动售货系统#
自动售货系统
https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf
import java.util.Scanner;
// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
private static void back(int nprices[], int notes[], int balance, int res[]){
int sum = 0;
for(int i=notes.length-1; i>=0; i--){
for(int j=0; j<notes[i]; j++){
if(sum+nprices[i]<balance){
notes[i]--;
res[i]++;
sum += nprices[i];
}else if(sum+nprices[i]==balance){
notes[i]--;
res[i]++;
sum += nprices[i];
return;
}
}
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String[] qmer = new String[6];
qmer[0] = "A1 2";
qmer[1] = "A2 3";
qmer[2] = "A3 4";
qmer[3] = "A4 5";
qmer[4] = "A5 8";
qmer[5] = "A6 6";
// 注意 hasNext 和 hasNextLine 的区别
while (in.hasNextLine()) { // 注意 while 处理多个 case
String str = in.nextLine();
String[] s = str.split(";");
//初始化
int[] goods = new int[6];
int[] notes = new int[4];
int[] nprice = new int[4];
nprice[0] = 1;
nprice[1] = 2;
nprice[2] = 5;
nprice[3] = 10;
int[] prices = new int[6];
prices[0] = 2;
prices[1] = 3;
prices[2] = 4;
prices[3] = 5;
prices[4] = 8;
prices[5] = 6;
int g_sum = 0;
int m_sum = 0;
int balance = 0;
char c= s[0].charAt(0);
if (c == 'r') {
String[] start = s[0].split(" ");
String[] price = start[1].split("-");
String[] note = start[2].split("-");
for (int i = 0; i < price.length; i++) {
goods[i] = Integer.parseInt(price[i]);
g_sum = g_sum + goods[i];
}
for (int i = 0; i < note.length; i++) {
notes[i] = Integer.parseInt(note[i]);
m_sum += (nprice[i] * notes[i]);
}
System.out.println("S001:Initialization is successful");
}
for (int i = 1; i < s.length; i++) {
if (s[i].charAt(0) == 'q') {
//查询
if (s[i].equals("q 0")) {
String[] qgoods = new String[6];
for (int j = 0; j < goods.length; j++) {
qgoods[j] = qmer[j] + " " + goods[j];
}
for (int j = 0; j < goods.length - 1; j++) {
for (int k = 0; k < goods.length - 1 - j; k++) {
if (goods[k] < goods[k + 1]) {
String tmp = qgoods[k];
qgoods[k] = qgoods[k + 1];
qgoods[k + 1] = tmp;
} else if (goods[k] == goods[k + 1]) {
if (qgoods[k].compareTo(qgoods[k + 1]) > 0) {
String tmp = qgoods[k];
qgoods[k] = qgoods[k + 1];
qgoods[k + 1] = tmp;
}
}
}
}
for (int j = 0; j < qgoods.length; j++) {
System.out.println(qgoods[j]);
}
}else if (s[i].equals("q 1")) {
System.out.println("1 yuan coin number=" + notes[0]);
System.out.println("2 yuan coin number=" + notes[1]);
System.out.println("5 yuan coin number=" + notes[2]);
System.out.println("10 yuan coin number=" + notes[3]);
}else {
System.out.println("E010:Parameter error");
}
}
if (s[i].charAt(0) == 'p') {
//投币
String[] money = s[i].split(" ");
int m = Integer.parseInt(money[1]);
if (m != 1 && m != 2 && m != 5 && m != 10) {
System.out.println("E002:Denomination error");
} else if((m!=1 && m!=2) && 1*notes[0]+2*notes[1]<m){
System.out.println("E003:Change is not enough, pay fail");
}else if (g_sum == 0) {
System.out.println("E005:All the goods sold out");
}else{
balance = balance + m;
if(m==1){
notes[0]+=1;
}else if(m==2){
notes[1]+=1;
}else if(m==5){
notes[2]+=1;
}else if(m==10){
notes[3]+=1;
}
System.out.println("S002:Pay success,balance=" + balance);
}
}
if(s[i].charAt(0) == 'b'){
//购买
String[] bg = s[i].split(" ");
int t = Integer.parseInt(bg[1].charAt(1)+"");
if(t<1 || t>6){
System.out.println("E006:Goods does not exist");
}else if(t>=1 && t<=6){
if(goods[t-1]==0){
System.out.println("E007:The goods sold out");
}else if(prices[t-1]>balance){
System.out.println("E008:Lack of balance");
}else{
goods[t-1] = goods[t-1] - 1;
balance = balance - prices[t-1];
System.out.println("S003:Buy success,balance=" + balance);
}
}
}
if(s[i].charAt(0)=='c'){
//退币
if(balance==0){
System.out.println("E009:Work failure");
}else{
int[] res = new int[4];
back(nprice, notes, balance, res);
for(int j=0; j<res.length; j++){
System.out.println(nprice[j] + " yuan coin number=" + res[j]);
}
balance = 0;
}
}
}
}
}
}