参加美团笔试的其中一个笔试题,题干是以角色扮演类游戏为背景,计算角色对标靶的总攻击和。
没有记录下详细的题干,只有自己的代码。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
| class Warrior: def __init__(self, a, d, c2, c3, c4): self.INITIAL_ATTACK = a self.MAX_ATTACK_BUFF_TIME = c2 self.MAX_AIM_DEFENCE = d self.MAX_DEFENCE_BUFF_TIME = c3 self.PERMANENCY_ATTACK_INCREASE = c4
self.baseAttack = self.INITIAL_ATTACK self.aimDefence = self.MAX_AIM_DEFENCE self.defenceBuffTime = 0 self.attackBuffTimes = []
self.totalAttack = 0
def do1(self): attack = (len(self.attackBuffTimes) + 1) * self.baseAttack
defence = self.MAX_AIM_DEFENCE if self.defenceBuffTime > 0: defence = 0
self.totalAttack += max(1, attack - defence) self.final()
def do2(self): self.attackBuffTimes.append(self.MAX_ATTACK_BUFF_TIME) self.final()
def do3(self): self.defenceBuffTime = self.MAX_DEFENCE_BUFF_TIME self.final()
def do4(self): self.baseAttack += self.PERMANENCY_ATTACK_INCREASE self.final()
def final(self): if 0 in self.attackBuffTimes: self.attackBuffTimes.remove(0) for i in range(len(self.attackBuffTimes)): self.attackBuffTimes[i] -= 1
if self.defenceBuffTime != 0: self.defenceBuffTime -= 0
if __name__ == '__main__': a = 3 d = 3 c2 = 6 c3 = 6 c4 = 2 opt = "121341" warrior = Warrior(a, d, c2, c3, c4) for c in opt: if c == '1': warrior.do1() elif c == '2': warrior.do2() elif c == '3': warrior.do3() elif c == '4': warrior.do4() print(warrior.totalAttack)
|