这道题在Python B C 组都有,应该属于国赛的打卡题

解题思路

  1. 用哈希表存储内存用量
  2. 判断输入的表达式为数组还是普通表达式
  3. 按照type添加对应量的内存
  4. 将内存从小到大转化
  5. 输出
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
t = eval(input())
memory = {'GB': 0, 'MB': 0, 'KB': 0, 'B': 0}

for _ in range(t):
s = input()
if '[]' in s:
byte = 4
i = 0
while i < len(s):
if s[i] == '[':
if s[i - 1] == 'g':
byte = 8
else:
byte = 4
i += 1
num = ""
while s[i] != ']':
num += s[i]
i += 1
if num:
memory['B'] += int(num) * byte
i += 1
else:
s = s.split(',')
if 'int' in s[0]:
memory['B'] += len(s) * 4
elif 'long' in s[0]:
memory['B'] += len(s) * 8
# string in s[0]
else:
for string in s:
memory['B'] += len(string.split('=')[-1]) - 2
memory['B'] -= 1

memory['KB'] += memory['B'] // 1024
memory['B'] %= 1024
memory['MB'] += memory['KB'] // 1024
memory['KB'] %= 1024
memory['GB'] += memory['MB'] // 1024
memory['MB'] %= 1024

res = ""
for k, v in memory.items():
if v > 0:
res += str(v) + k

print(res)