题解 | #自动售货系统#

自动售货系统

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

根据题意一步一步走,本题并没有难度,我的代码使用了大量的分支判断,性能太低了,期待大佬给出更好的答案

const indexToTypes = {
    0: 'A1',
    1: 'A2',
    2: 'A3',
    3: 'A4',
    4: 'A5',
    5: 'A6'
}
const indexToChangeTypes = {
    0: '1',
    1: '2',
    2: '5',
    3: '10'
}
const prices = {
    'A1': 2,
    'A2': 3,
    'A3': 4,
    'A4': 5,
    'A5': 8,
    'A6': 6
}
while(line = readline()) {
    const str = line.split(';');
    let balance = 0;
    const goods = {};
    const changes = {};
    let count = 0;
    
    for(let i = 0; i < str.length; i++) {
        const temp = str[i].split(' ');
        // 初始化命令获得商品和存钱数
        if(temp[0] == 'r') {
            let cgs = temp[1].split('-');
            let ccs = temp[2].split('-');
            for(let j = 0; j < cgs.length; j++) {
                const num = parseInt(cgs[j]);
                count += num
                goods[indexToTypes[j]] = num
            }
            for(let j = 0; j < ccs.length; j++) {
                changes[indexToChangeTypes[j]] = parseInt(ccs[j])
            }
            print('S001:Initialization is successful')
        } else if(temp[0] == 'c') {
            // 针对balance 进行货币计算
            if(balance == 0) {
                print("E009:Work failure")
            } else {
                // 退币原则
                const res = [];
             
                for(let j = 3; j >= 0; j--) {
                    const p = parseInt(indexToChangeTypes[j]);
                    // 需要p面额的多少张
                    const count = Math.floor(balance / p);
                    //print(count, changes[p])
                    if(count >= changes[p]) {
                        balance -= p * parseInt(changes[p]);
                        res.push(changes[p]);
                        
                    } else {
                        res.push(count);
                        balance -= p * count;
                    }
                }
                for(let j = 3; j >= 0; j--) {
                    print(`${indexToChangeTypes[3-j]} yuan coin number=${res[j]}`)
                }
                
            }
        } else if(temp[0] == 'b') {
            // 开始购物
            if(prices[temp[1]]) {
                const price = prices[temp[1]];
                if(goods[temp[1]] == 0) {
                    print("E007:The goods sold out")
                } else if(balance >= price) {
                    balance -= price
                    print('S003:Buy success,balance=' + balance)
                } else {
                    print('E008:Lack of balance')
                }
            } else {
                print("E006:Goods does not exist")
            }
            
        } else if(temp[0] == 'p') {
            // p 钱币面额  进行投币  如果货物为空,禁止投币
            const money = parseInt(temp[1])
            if(money != 1 && money != 2 && money != 5 && money != 10 ) {
                print("E002:Denomination error")
            } else if(count === 0) {
                print("E005:All the goods sold out")
            } else if(money <= 2) {
                balance += parseInt(temp[1])
                const key = Object.keys(changes).filter(o => changes[o] == money)[0]
                changes[key] += 1
                print("S002:Pay success,balance=" + balance)
            } else if(parseInt(changes[1]) + 2 * parseInt(changes[2]) < parseInt(temp[1])) {
                print("E003:Change is not enough, pay fail")
            } else {
                balance += parseInt(temp[1])
                const key = Object.keys(changes).filter(o => changes[o] == money)[0]
                changes[key] += 1
                print("S002:Pay success,balance=" + balance)
            }
        } else if(str[i][0] == 'q') {
            if(temp[1] == '0') {
                Object.keys(goods).map(o => ({
                    name: o,
                    count: goods[o],
                    price: prices[o]
                })).sort((a, b) => a.count == b.count ? b.name - a.name : b.count - a.count).forEach(k => print(`${k.name} ${k.price} ${k.count}`))
            } else if(temp[1] == '1') {
                Object.keys(changes).forEach(k => print(`${k} yuan coin number=${changes[k]}`))
            } else {
                print("E010:Parameter error")
            }
        }
    }
    // r 任一阶段可以重置系统状态
    // p 钱币面额
    // b  购买商品
    // c  退币
}
全部评论

相关推荐

点赞 评论 收藏
分享
不愿透露姓名的神秘牛友
10-05 10:13
已编辑
HHHHaos:让这些老登来现在秋招一下,简历都过不去
点赞 评论 收藏
分享
1 收藏 评论
分享
牛客网
牛客企业服务