技术问询:计算达成指定胜率所需的额外获胜场次及代码修正
计算达到指定胜率所需的额外获胜场次
我正在尝试计算需要多少额外获胜场次才能达到指定的胜负比,但搞不清计算逻辑。已知当前胜率和总战斗场次,比如总战斗场次112场,当前胜率50%,需要多少额外胜利才能达到70%胜率?我写了下面的Swift代码,但没得到预期结果,或许有更简便的计算方式。
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. 代码核心错误
你的代码里胜率计算逻辑完全错误:
let newPercentage = currentBattlesInt * totalWins
正确的胜率计算应该是 (总胜场 / 总场次) * 100,而不是总场次乘以总胜场,这直接导致第一次循环就得到了6441这种离谱数值,逻辑彻底走偏。
2. 高效数学解法(无需循环)
设需要额外获胜x场,根据胜率要求列方程:
- 当前胜场:
currentWins = 当前胜率(小数) * 总场次 - 目标胜率:
targetRatio(比如70%就是0.7) - 目标状态等式:
(currentWins + x) / (总场次 + x) = targetRatio
解方程可得:
x = (targetRatio * 总场次 - currentWins) / (1 - targetRatio)
因为场次必须是整数,最终结果需要向上取整(哪怕小数部分小于1,也需要多打一场才能达标)。
3. 修正后的Swift代码
func calculateNeededWins() { guard let currentRatioStr = currentRatio, let currentRatioDouble = Double(currentRatioStr), let currentBattlesStr = currentBattles, let currentBattles = Double(currentBattlesStr), let targetRatioStr = neededRatio, let targetRatioDouble = Double(targetRatioStr) else { return } let currentRatioDecimal = currentRatioDouble / 100 let currentWins = currentRatioDecimal * currentBattles let targetRatio = targetRatioDouble / 100 // 公式计算所需额外胜场 let numerator = targetRatio * currentBattles - currentWins let denominator = 1 - targetRatio guard denominator != 0 else { // 特殊情况:目标胜率100%,需要剩下的所有场次都赢 neededWinsString = "\(Int(currentBattles - currentWins))" currentWinsString = "\(Int(currentWins))" return } let neededWins = ceil(numerator / denominator) // 确保结果非负(当前胜率已达标时无需额外场次) neededWinsString = "\(max(Int(neededWins), 0))" currentWinsString = "\(Int(currentWins))" }
4. 示例验证
用你给出的例子:
- 当前总场次112,胜率50% → 当前胜场56
- 目标胜率70% → 代入公式:
x = (0.7*112 -56)/(1-0.7) = (78.4 -56)/0.3 ≈74.67
向上取整后为75场,此时总胜场56+75=131,总场次112+75=187,131/187≈70.05%,刚好达标。
内容的提问来源于stack exchange,提问作者Ceri Turner
相关产品推荐
相关产品推荐

