题解 | #自动售货系统#

自动售货系统

https://www.nowcoder.com/practice/cd82dc8a4727404ca5d32fcb487c50bf

本题逻辑并不复杂, 但是条件,逻辑非常多
主要有四个难点
1: 字符串的切割, 不要搞乱了, 尤其是对于一些非法输入的处理
2: 余额,钱币数量的维护
3: 数量从大到小排序, 但是数量相同的名称在前, 就是稳定排序
4: 退币原则, 使用贪心算法算出

但是本题最难的是考试的时候不会提供测试用例, 所以基本上做不出来, 而且做出来了也需要调试很久, 得不偿失


import javax.swing.plaf.metal.MetalIconFactory;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Scanner;

public class Main
{
    //S001:Initialization is successful
    //E009:Work failure
    //E010:Parameter error
    //S002:Pay success,balance=1
    //E008:Lack of balance
    //1 yuan coin number=1
    //2 yuan coin number=0
    //5 yuan coin number=0
    //10 yuan coin number=0
    //E008:Lack of balance
    //E008:Lack of balance
    //E009:Work failure
    //E010:Parameter error
    //S002:Pay success,balance=5
    public static int []money = new int[5];
    public static void main(String[] args)
    {
        Scanner sc = new Scanner(System.in);
        String str[] = sc.nextLine().split(";");
        //  r 22-18-21-21-7-20 3-23-10-6;c;q0;p 1;b A6;c;b A5;b A1;c;q1;p 5;
        //                      0        1 2  3    4   5   6   7   8 9   10
        //r A1 数量 -A2 数量 -A3 数量 -A4 数量 -A5 数量 -A6 数量 1 元张数 -2 元张数 -5 元张数 -10 元张数
        int []shuliang = new int[7];
        shuliang[0] = -1;
        int []danjia = { 0, 2,3,4,5,8,6};


        String initial[] = str[0].split(" ");
        String shuliangStr[] = initial[1].split("-");
        String moneyStr[] = initial[2].split("-");
        ArrayList<String> list = new ArrayList<>();
        //商品数量初始化
        shuliang[0] = 0;
        for (int i = 1; i <= 6; i++)
        {
            shuliang[i] = Integer.parseInt(shuliangStr[i-1]);
        }
        //钱的张数初始化
        for (int i = 1; i <=4 ; i++)
        {
            money[i] = Integer.parseInt(moneyStr[i-1]);
        }
        list.add("S001:Initialization is successful");

        int toubi = 0;


        for (int i = 1; i < str.length; i++)
        {
            switch (str[i].charAt(0))
            {
                case 'p':
                    String []p = str[i].split(" ");
                    int cur = Integer.parseInt(p[1]);

                    if(cur !=1 && cur !=2 && cur !=5 && cur !=10 )
                    {
                        list.add("E002:Denomination error");
                        break;
                    }
                    if( cur !=1 && cur !=2 && money[1]+money[2]*2 < cur )
                    {
                        list.add("E003:Change is not enough, pay fail");
                        break;
                    }
                    if ( shuliang[1] == 0 && shuliang[2] == 0 && shuliang[3] == 0
                            && shuliang[4] == 0 && shuliang[5] == 0 && shuliang[6] == 0 )
                    {
                        list.add("E005:All the goods sold out");
                        break;
                    }
                    toubi += cur;
				//易错: 投币后要对钱币张数进行维护
                    switch (cur)
                    {
                        case 1:
                            money[1]++;
                            break;
                        case 2:
                            money[2]++;
                            break;
                        case 5:
                            money[3]++;
                            break;
                        case 10:
                            money[4]++;
                            break;
                    }
                    list.add("S002:Pay success,balance="+ toubi);
                    break;

                case 'b':
                    String []buy = str[i].split(" ");
                    int index = Integer.parseInt( buy[1].substring(1) );

                    if( !buy[1].equals("A1") && !buy[1].equals("A2") && !buy[1].equals("A3")
                            && !buy[1].equals("A4") && !buy[1].equals("A5") && !buy[1].equals("A6")  )
                    {
                        list.add("E006:Goods does not exist");
                        break;
                    }
                    if(shuliang[index] ==0)
                    {
                        list.add("E007:The goods sold out");
                        break;
                    }
                    if( toubi < danjia[index])
                    {
                        list.add("E008:Lack of balance");
                        break;
                    }
				//toubi 表示余额, 全局都是需要维护的
                    toubi -= danjia[index];
                    list.add("S003:Buy success,balance=" + toubi);

                    break;
                case 'c':
                    if(toubi == 0)
                    {
                        list.add("E009:Work failure");
                        break;
                    }
                    else if(toubi > 0)
                    {
                        int []tuibi = FuncTuibi(toubi);
                        int []jine = new int[]{0,1,2,5,10};
                        //1 yuan coin number=0
                        for (int j = 1; j <= 4 ; j++)
                        {
                            list.add(jine[j] + " yuan coin number=" + tuibi[j]);

                        }
                        toubi = 0;
                        break;
                    }
                    break;
                case 'q':
				//对非法输入的处理
                    if( str[i].indexOf(" ") == -1 )
                    {
                        list.add("E010:Parameter error");
                        break;
                    }
                    String []chaxun = str[i].split(" ");
                    if( !chaxun[1].equals("0") && !chaxun[1].equals("1"))
                    {
                        list.add("E010:Parameter error");
                        break;
                    }
                    if(chaxun[1].equals("1"))
                    {
                        list.add("1 yuan coin number="+money[1]);
                        list.add("2 yuan coin number="+money[2]);
                        list.add("5 yuan coin number="+money[3]);
                        list.add("10 yuan coin number="+money[4]);
                        break;
                    }
                    int [][] good = new int[7][2];
                    for (int j = 0; j < 7; j++)
                    {
                        good[j][0] = j;
                        good[j][1] = shuliang[j];
                    }
				//用Arrays.sort() 对二维数组的行进行稳定排序
                    Arrays.sort(good, new Comparator<int[]>()
                    {
                        @Override
                        public int compare(int[] o1, int[] o2)
                        {
                            return o2[0] - o1[0];
                        }
                    });
                    for (int j = 6; j >=0; j--)
                    {
                        int index1 = good[j][0];
                        // A1 2 6
                        if(index1 == 0)
                        {
                            continue;
                        }

                        list.add("A"+index1 + " " + danjia[index1] + " " + shuliang[index1]);
                    }

                    break;

            }
        }

        for(String i: list)
        {
            System.out.println(i);
        }



    }
//贪心算法实现最少张数退币
    public static int[] FuncTuibi( int toubi )
    {
        int []tuibi = new int[5];
        Arrays.fill(tuibi,0);
        int []jine = {0,1,2,5,10};
        for (int i = 4; i >0 ; i--)
        {
            int shuliang = toubi/jine[i];
            if(shuliang == 0)
            {
                continue;
            }
            if(money[i] >= shuliang)
            {
                tuibi[i] = shuliang;
                money[i] = money[i] - shuliang;
                toubi -= shuliang * jine[i];
            }
            else if( money[i] < shuliang )
            {
                tuibi[i] = money[i];
                money[i] = 0;
                toubi -= money[i] * jine[i];
            }
        }
return tuibi;
    }
}

华为机试题解 文章被收录于专栏

华为机试题解

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务