自动售货机

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);
		}
	}
}

 

转载于:https://my.oschina.net/u/2444659/blog/732121

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值