自动售货机
自动售货机
笨拙的小Q 发表于1年前
自动售货机
  • 发表于 1年前
  • 阅读 32
  • 收藏 0
  • 点赞 0
  • 评论 0

腾讯云 技术升级10大核心产品年终让利>>>   

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

 

共有 人打赏支持
粉丝 2
博文 30
码字总数 27842
×
笨拙的小Q
如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
* 金额(元)
¥1 ¥5 ¥10 ¥20 其他金额
打赏人
留言
* 支付类型
微信扫码支付
打赏金额:
已支付成功
打赏金额: