## 自动售货机 原

笨拙的小Q

``````import java.util.Scanner;
/**
* 华为机试 自动售货机
* 不知道对错
* @author qxl
*
*/
public class Shop {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int price1 = 2, s_count1 = 5;
int price2 = 3, s_count2 = 10;
int price3 = 4, s_count3 = 14;//初始化
int y_count1 = 5;
int y_count2 = 5;
int y_count5 = 3;
while (sc.hasNext()) {
String s = sc.nextLine();//p 5,p 5,p 5,b A3,b A3,b A2
s = s.replace(" ", ",");//p,5,p,5,p,5,b,A3,b,A3,b,A2
String[]str = s.split(",");//{"p","5","p","5","p","5","b","A3","b","A3","b","A2"}
int total_pay = 0;
int total_cost = 0;
for(int i=0;i<str.length;i+=2){
if(str[i].equals("p")){
total_pay+=Integer.parseInt(str[i+1]);
}
if(str[i].equals("b")){
if(str[i+1].equals("A1")){
total_cost+=price1;
s_count1--;
}
if(str[i+1].equals("A2")){
total_cost+=price2;
s_count2--;
}
if(str[i+1].equals("A3")){
total_cost+=price3;
s_count3--;
}
}
}
int remain = total_pay - total_cost;//找钱
int i5=0,i2=0,i1=0;//分别找5、2、1元钱的张数
i5 = remain/5;
if(i5<y_count5){
remain = remain-i5*5;
}else{
remain = remain-y_count5*5;
}
if(remain!=0){
i2 = remain/2;
if(i2<y_count2){
remain = remain - i2*2;
}else{
remain = remain - y_count2*2;
}
}
if(remain!=0){
i1 = remain/1;//不考虑非法输入，一定能找零。
}
int lost_count1 = y_count1 - i1;//存钱盒中剩余1块钱的数量
int lost_count2 = y_count2 - i2;//存钱盒中剩余2块钱的数量
int lost_count5 = y_count5 - i5;//存钱盒中剩余5块钱的数量
String s1 = "A1" + " " + s_count1 + ",";
String s2 = "A2" + " " + s_count2 + ",";
String s3 = "A3" + " " + s_count3 + ",";
String s4 = "1" + " " + lost_count1 + ",";
String s5 = "2" + " " + lost_count2 + ",";
String s6 = "5" + " " + lost_count5;

System.out.println(s1+s2+s3+s4+s5+s6);
}
}
}``````

### 笨拙的小Q

06/28
0
0

智察网讯 作为国民支付手段的微信支付，正大踏步的迈向海外市场。12月25日消息，据外媒报道，日本饮料企业伊藤园与开发智能手机相关服务的NEOS共同开发出一款面向访日中国游客、可用智能...

2017/12/25
0
0

2017/12/27
0
0

12/03
0
0

2017/12/02
0
0

iOS 官方文档

walking_yxf
4分钟前
0
0

5分钟前
0
0
Aries数据库事务Recovery算法

8分钟前
0
0
Rxjava Backpressure 32

woshixin
8分钟前
0
0
IDEA-Create Git Repository

1、概述 idea 开发完毕的项目没有及时的关联gitlab，如果整体项目关联gitlab。 2、干 2.1 gitlab 创建项目 2.2 idea 1、IDEA 点击 -> VCS -> import into version control -> create git re......

12分钟前
0
0