美文网首页
华为OD机试真题2023_Swift_100_网上商城优惠活动(

华为OD机试真题2023_Swift_100_网上商城优惠活动(

作者: 雾中探雪 | 来源:发表于2023-03-09 20:53 被阅读0次
    // 网上商城优惠活动(一)
        func HW2023016() {
            var moneyArr: [Int] = []
            // 测试用例
    //        let youhui = "3 2 5"
    //        moneyArr = [100, 200, 400]
            
            //  开始代码
            let youhui = String(readLine()!)
            let _ = readLine()
            while let line = readLine() {
                moneyArr.append(Int(line)!)
            }
            
            let youhuiquan = youhui.components(separatedBy: " ").map { Int($0) ?? 0 }
            let manjian = youhuiquan[0]  // 满减券数量
            let dazhe = youhuiquan[1]    // 打折券数量
            let wumenkan = youhuiquan[2] // 无门槛券数量
            for money in moneyArr {
                var resDic: [Double: Int] = [:]
                let quanMJ = money/100 > manjian ? manjian : Int(money/100)
                var afterDZ: Double = 0
                var afterMJ: Double = 0
                var dazheWMK: Double = 0
                
                if dazhe > 0 {  // 有打折券的情况
                    // 先满减后打折
                    afterDZ = manJian(Double(money), manjian)
                    resDic.updateValue(quanMJ+1, forKey: afterDZ)
                    
                    // 先打折后满减
                    afterMJ = manJian(Double(money)*0.92, manjian)
                    let quanMJAfterDZ = Double(money)*0.92/100 > Double(manjian) ? manjian : Int(Double(money)*0.92/100)
                    if resDic.keys.contains(afterMJ) {
                        let temp = resDic[afterMJ]!
                        if temp > (quanMJAfterDZ+1) {
                            resDic.updateValue(quanMJAfterDZ+1, forKey: afterMJ)
                        }else {
                            resDic.updateValue(temp, forKey: afterMJ)
                        }
                    }else {
                        resDic.updateValue(quanMJAfterDZ+1, forKey: afterMJ)
                    }
                    
                    // 先打折后无门槛
                    dazheWMK = Double(money) * 0.92 - Double(wumenkan * 5)
                    if resDic.keys.contains(dazheWMK) {
                        let temp = resDic[dazheWMK]!
                        if temp > (wumenkan+1) {
                            resDic.updateValue(wumenkan+1, forKey: dazheWMK)
                        }else {
                            resDic.updateValue(temp, forKey: dazheWMK)
                        }
                    }else {
                        resDic.updateValue(wumenkan+1 , forKey: dazheWMK)
                    }
                }
                // 先满减后无门槛
                let manjianWMK = manJian(Double(money), manjian) - Double(wumenkan * 5)
                if resDic.keys.contains(manjianWMK) {
                    let temp = resDic[manjianWMK]!
                    if temp > (quanMJ+wumenkan) {
                        resDic.updateValue(quanMJ+wumenkan, forKey: manjianWMK)
                    }else {
                        resDic.updateValue(temp, forKey: manjianWMK)
                    }
                }else {
                    resDic.updateValue(quanMJ+wumenkan, forKey: manjianWMK)
                }
                let minPrice: Double = min(min(afterDZ, afterMJ), min(manjianWMK, dazheWMK))
                print("\(Int(floor(minPrice))) \(Int(resDic[minPrice]!))")
            }
        }
        func manJian(_ money: Double, _ manjian: Int) -> Double {
            if Int(money/100) >= manjian {
                return money - Double(manjian*10)
            }else {
                return money - Double((money/100)*10)
            }
        }
    

    相关文章

      网友评论

          本文标题:华为OD机试真题2023_Swift_100_网上商城优惠活动(

          本文链接:https://www.haomeiwen.com/subject/vxjnldtx.html