我试图计算出需要多少更多的胜利才能得到一定的赢/输比,但我不能让我的头周围如何工作出来。
我有胜率和总战斗次数
比如我有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
1条答案
按热度按时间gojuced71#
定义几个变量:
使用一个小代数,我们可以推导出解决方案:
显然,我们需要将答案四舍五入到最接近的整数。
因此,给定112场战斗,当前运行率为50%,要将该运行率提高到70%,您需要75连胜。
因此,Swift中的简单解决方案是:
您可能应该检查先决条件并处理边缘情况场景,例如:
FWIW,算法解决方案可能是:
但算术解显然更有效。