這篇主要是記錄一些考點為流程控制(Control Flow)的題目

a349. 2. 指令解譯器

只要能弄清楚每種指令在做什麼,那就沒什麼問題了。

AC (24ms, 3.4MB)

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
try:
while 1:
r, m = [0,0,0,0], []
# 輸入八個記憶體位址
for i in range(8):
m.append(int(input()))

# 操作n筆指令
n = int(input())
for i in range(n):
code = list(map(str, input().split()))

# load
if code[0]=='LOAD':
r[int(code[1])] = m[int(code[2])]

# store
if code[0]=='STORE':
m[int(code[1])] = r[int(code[2])]

# add
if code[0]=='ADD':
r[int(code[1])] = r[int(code[2])] + r[int(code[3])]

# move
if code[0]=='MOVE':
r[int(code[1])] = r[int(code[2])]

# 輸出
print(r[0])
print(m[0])
except EOFError:
pass

封面圖源:Pixiv