swift 计算多少点,以获得一个设定的百分比

iyfjxgzm  于 2023-05-16  发布在  Swift
关注(0)|答案(1)|浏览(115)

我试图计算出需要多少更多的胜利才能得到一定的赢/输比,但我不能让我的头周围如何工作出来。
我有胜率和总战斗次数
比如我有112场战斗,胜率50%,我需要再赢多少场才能达到70%
到目前为止,我有下面的,但我没有得到我所期望的值。也许还有更简单的方法来做到这一点。

func calculateNeededWins() {
        
        // Convert to Double
        guard let currentRatioDouble = Double(currentRatio) else {return}
        guard var currentBattlesInt = Double(currentBattles) else {return}
        
        NSLog("Current Win Ratio: \(currentRatioDouble)")
        NSLog("Total Battles: \(currentBattlesInt)")
        
        // Convert Ratio to Decimal
        let currentRatioDecimal = (currentRatioDouble / 100)
        NSLog("Current Win Ratio (Decimal): \(currentRatioDecimal)")
        
        var totalWins = currentRatioDecimal * currentBattlesInt
        NSLog("Total Wins: \(totalWins)")
        NSLog("------------")
        let currentWins = totalWins
        guard var currentPercentage = Int(currentRatio) else {return}
        
        let needRatio = neededRatio * 10
        var needWins = 0
        
        NSLog("Loop Start")
        // Add 1 to both wins & total battles until win ratio = required
        while(currentPercentage <= needRatio) {
            NSLog("Current %: \(currentPercentage)")
            NSLog("Wins %: \(totalWins)")
            NSLog("Total %: \(currentBattlesInt)")
            NSLog("------------")
            
            currentBattlesInt += 1
            totalWins += 1
            needWins += 1
            
            let newPercentage = currentBattlesInt * totalWins
            NSLog("New %: \(newPercentage)")
            NSLog("Wins %: \(totalWins)")
            NSLog("Total %: \(currentBattlesInt)")
            
            NSLog("************************************************************")
            
            currentPercentage = Int(newPercentage)
        }
        
        neededWinsString = "\(needWins)"
        
        currentWinsString = "\(currentWins)"
    }

这导致
当前胜率:50.0
总战役:112.0
当前中奖率(十进制):0.5
总胜率:56.0
循环开始
当前:50
胜:56.0
总计:112.0
新产品:6441.0
胜:57.0
总计:113.0

gojuced7

gojuced71#

定义几个变量:

  • rt =目标胜率
  • rc =当前胜率
  • bc =到目前为止有多少战斗
  • wa =需要多少额外的胜利

使用一个小代数,我们可以推导出解决方案:

  • wa* =((rt - rcbc)/(1 - rt

显然,我们需要将答案四舍五入到最接近的整数。
因此,给定112场战斗,当前运行率为50%,要将该运行率提高到70%,您需要75连胜。
因此,Swift中的简单解决方案是:

func additionalWins(currentRate: Double, targetRate: Double, battles: Int) -> Int {
    let result = ((targetRate - currentRate) * Double(battles)) / (1 - targetRate)
    return max(0, Int(result.rounded(.up)))
}

您可能应该检查先决条件并处理边缘情况场景,例如:

func additionalWins(currentRate: Double, targetRate: Double, battles: Int) -> Int {
    precondition(0...1 ~= currentRate)
    precondition(0...1 ~= targetRate)
    precondition(battles >= 0)
    
    if targetRate <= currentRate { 
        return 0
    }
    
    if battles == 0 {
        return targetRate > 0 ? 1 : 0
    }
        
    if targetRate == 1 {
        if currentRate == 1 {
            return 0
        }
        fatalError("There is no way, given current rate of \(currentRate * 100)%, that you can achieve target rate of \(targetRate * 100)%")
    }
        
    let result = ((targetRate - currentRate) * Double(battles)) / (1 - targetRate)
    return max(0, Int(result.rounded(.up)))
}

FWIW,算法解决方案可能是:

func additionalWins(currentRate: Double, targetRate: Double, battles: Int) -> Int {
    precondition(0...1 ~= currentRate)
    precondition(0...1 ~= targetRate)
    precondition(battles >= 0)
    
    if targetRate <= currentRate { 
        return 0
    }
    
    if battles == 0 {
        return targetRate > 0 ? 1 : 0
    }
        
    if targetRate == 1 {
        if currentRate == 1 {
            return 0
        }
        fatalError("There is no way, given current rate of \(currentRate * 100)%, that you can achieve target rate of \(targetRate * 100)%")
    }

    var i = 0.0
    var currentWins = (Double(battles) * currentRate).rounded()
    var currentBattles = Double(battles)
    
    while ((currentWins + i) / (currentBattles + i)) < targetRate {
        i += 1
    }
    
    return Int(i)
}

但算术解显然更有效。

相关问题