美团2021.8.15的一个笔试题

参加美团笔试的其中一个笔试题,题干是以角色扮演类游戏为背景,计算角色对标靶的总攻击和。
没有记录下详细的题干,只有自己的代码。

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 # 进攻buff 持续时间
self.MAX_AIM_DEFENCE = d # 目标最大防御力
self.MAX_DEFENCE_BUFF_TIME = c3 # 减防buff持续时间
self.PERMANENCY_ATTACK_INCREASE = c4 # 永久提升额

self.baseAttack = self.INITIAL_ATTACK # 基础攻击力
self.aimDefence = self.MAX_AIM_DEFENCE # 目标防御力
self.defenceBuffTime = 0 # 减防buff剩余时间
self.attackBuffTimes = [] # 攻击buff剩余时间

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):
# 增加buff
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):
# buff结算
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)
# while True:
# try:
# n, a, d, c2, c3, c4 = [int(i) for i in input().split()]
# opt = input()
# ans = solve(a, d, c2, c3, c4, opt)
# print(ans)
# except:
# break