题解 | 回合制游戏
回合制游戏
https://www.nowcoder.com/practice/17a083854661490e85e5bb6c4b26e546
def min_turns_to_defeat(hp, na, ba):
# 如果普通攻击就能一回合击败敌人,直接返回1
if hp <= na:
return 1
# 如果两倍的聚力攻击能击败敌人,考虑是否使用聚力
if hp <= 2 * na:
return 2
# 计算不使用聚力需要的最小回合数
r1 = hp // na + (hp % na != 0)
# 计算使用聚力需要的最小回合数
r2 = hp // ba * 2 # 聚力一次需要2个回合;
#print("r2:", r2)
if hp % ba != 0:
if hp % ba <= na:
r2 += 1
else:
r2 += 2
#print("r1:", r1, r2)
# 返回两种策略中的最小回合数
return min(r1, r2)
# 读取输入
HP = int(input().strip())
normalAttack = int(input().strip())
buffedAttack = int(input().strip())
# 计算并输出结果
result = min_turns_to_defeat(HP, normalAttack, buffedAttack)
print(result)
查看8道真题和解析
