首页 > 试题广场 >

回合制游戏

[编程题]回合制游戏
  • 热度指数:7321 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 32M,其他语言64M
  • 算法知识视频讲解
你在玩一个回合制角色扮演的游戏。现在你在准备一个策略,以便在最短的回合内击败敌方角色。在战斗开始时,敌人拥有HP格血量。当血量小于等于0时,敌人死去。一个缺乏经验的玩家可能简单地尝试每个回合都攻击。但是你知道辅助技能的重要性。
在你的每个回合开始时你可以选择以下两个动作之一:聚力或者攻击。
    聚力会提高你下个回合攻击的伤害。
    攻击会对敌人造成一定量的伤害。如果你上个回合使用了聚力,那这次攻击会对敌人造成buffedAttack点伤害。否则,会造成normalAttack点伤害。
给出血量HP和不同攻击的伤害,buffedAttack和normalAttack,返回你能杀死敌人的最小回合数。

输入描述:
第一行是一个数字HP
第二行是一个数字normalAttack
第三行是一个数字buffedAttack
1 <= HP,buffedAttack,normalAttack <= 10^9


输出描述:
输出一个数字表示最小回合数
示例1

输入

13
3
5

输出

5
h = int(input())
n = int(input())
b = int(input())

if b < 2 * n:
    if h % n != 0:
        print(h // n + 1)
    else:
        print(h // n)
else:
    y = h % b
    if y > n:
        print(2 * (h // b) + 2)
    elif y == 0:
        print(2 * (h // b))
    else:
        print(2 * (h // b) + 1)
发表于 2019-09-06 17:09:52 回复(0)
hp = int(input())
normal_attack = int(input())
buffered_attack = int(input())
if buffered_attack <= 2 * normal_attack:
    print((hp-1) // normal_attack + 1)
else:
    nums_buf = (hp-1) // buffered_attack 
    end = hp % buffered_attack
    if end == 0:
        nums_buf += 1
        print(2*nums_buf)
    elif end <= normal_attack:
        print(2*nums_buf + 1)
    else:
        nums_buf += 1
        print(2*nums_buf)
发表于 2019-09-06 15:12:46 回复(0)
分成四种情况:
import math
h = int(input())
n = int(input())
b = int(input())

if h // n == 0:
    print(1)
elif 2*n > b:
    print(math.ceil(h/n))
elif h%b > n or h%b == 0:
    print(math.ceil(h/b)*2)
else:
    print(math.ceil(h/b)*2-1)


运行时间:30ms
占用内存:3564k
发表于 2019-09-02 14:21:18 回复(0)
"""
分支判断
"""
import math

if __name__ == "__main__":
    hp = int(input().strip())
    normalAttack = int(input().strip())
    buffedAttack = int(input().strip())
    ans = 0
    if 2 * normalAttack >= buffedAttack:
        ans = math.ceil(hp / normalAttack)
    else:
        ans = math.floor(hp / buffedAttack) * 2
        hp = hp % buffedAttack
        if hp > 0:
            if hp <= normalAttack:
                ans += 1
            else:
                ans += 2
    print(ans)

发表于 2019-07-17 22:03:46 回复(0)