计算还需要多少分数才能达到特定百分比。

huangapple go评论61阅读模式
英文:

Calculate how many more points to get a set percentage

问题

I'm trying to work out how many more wins are needed to get a certain win/loss ratio, but I can't get my head around how to work it out.

我试图计算需要多少更多的胜利才能达到特定的胜率,但我无法弄清楚如何计算。

I have the win ratio, and total number of battles

我有胜率和总战斗次数

For example, I have 112 battles, and a 50% win rate, how many more wins do I need to get to 70%

例如,我有112场战斗,胜率为50%,我需要多少更多的胜利才能达到70%

So far I have the below, but I'm not getting the values I'm expecting. There's probably an easier way of doing this as well.

到目前为止,我有以下内容,但我得到的值与我期望的不符。可能还有更简单的方法。

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

This results with

>Current Win Ratio: 50.0

>Total Battles: 112.0

>Current Win Ratio (Decimal): 0.5

>Total Wins: 56.0

>------------

>Loop Start

>Current : 50

>Wins : 56.0

>Total : 112.0

>------------

>New : 6441.0

>Wins : 57.0

>Total : 113.0

英文:

I'm trying to work out how many more wins are needed to get a certain win/loss ratio, but I can't get my head around how to work it out.

I have the win ratio, and total number of battles

For example, I have 112 battles, and a 50% win rate, how many more wins do I need to get to 70%

So far I have the below, but I'm not getting the values I'm expecting. There's probably an easier way of doing this as well.

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

This results with

>Current Win Ratio: 50.0

>Total Battles: 112.0

>Current Win Ratio (Decimal): 0.5

>Total Wins: 56.0

>------------

>Loop Start

>Current : 50

>Wins : 56.0

>Total : 112.0

>------------

>New : 6441.0

>Wins : 57.0

>Total : 113.0

答案1

得分: 1

定义一些变量:

  • r<sub>t</sub> = 目标胜率
  • r<sub>c</sub> = 当前胜率
  • b<sub>c</sub> = 至今进行的战斗数
  • w<sub>a</sub> = 需要额外获胜的数量

通过一些代数运算,我们可以推断出解决方案:

w<sub>a</sub> = ((r<sub>t</sub> - _r<sub>c</sub>) ⨉ b<sub>c</sub>) / (1 - r<sub>t</sub>)

显然,我们需要将答案四舍五入到最接近的整数。

因此,假设进行了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("无论当前胜率为\(currentRate * 100)%,你都无法达到目标胜率\(targetRate * 100)%")
    }
        
    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("无论当前胜率为\(currentRate * 100)%,你都无法达到目标胜率\(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)
}

但显然,算术解决方案更高效。

英文:

Defining a few variables:

  • r<sub>t</sub> = target win rate
  • r<sub>c</sub> = current win rate
  • b<sub>c</sub> = how many battles thus far
  • w<sub>a</sub> = how many additional wins are needed

Using a little algebra, we can deduce the solution:

> w<sub>a</sub> = ((r<sub>t</sub> - r<sub>c</sub>) ⨉ b<sub>c</sub>) / (1 - r<sub>t</sub>)

And, obviously, we then need to round that answer up to the nearest integer.

Thus, given 112 battles with a current run rate of 50%, to get that rate to 70% you’d need 75 straight wins.

Thus, the simple solution in Swift is:

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

You probably should check the preconditions and handle edge case scenarios, perhaps:

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

FWIW, an algorithmic solution might be:

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

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

But the arithmetic solution is obviously more efficient.

huangapple
  • 本文由 发表于 2023年5月15日 02:59:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76249219.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定