第 36 关,字母排序。指令集介绍请看:https://www.annhe.net/article-3828.html。
优化目标
39行109步
代码
27行64步
此方案见 Github。说明如下:
- 首先将第一个词读入
- 巧妙的利用
INBOX
中的0
初始化22
号格子 - 然后依次读入第二个词的字符,和第一个词对应位置比较,直接输出较小的
- 通过
22
号减23
号(23
号为第一个词的长度)判断长度,只有22号-23号
为负的情况下(即第二个词较短),才有必要继续循环下去,如果不为负,说明第一个字符串已经结束了,那么程序也应该结束了。
流程图如下:
代码:
first:
INBOX
COPYTO [23]
JUMPZ second
BUMPUP 23
JUMP first
second:
COPYTO 22
input:
INBOX
JUMPZ end
COPYTO 20
SUB [22]
JUMPZ out2
JUMPN output2
output1:
COPYFROM [22]
JUMPZ end
OUTBOX
BUMPUP 22
JUMP output1
output2:
COPYFROM 20
o2:
OUTBOX
INBOX
JUMPZ end
JUMP o2
out2:
COPYFROM 20
OUTBOX
BUMPUP 22
SUB 23
JUMPN input
end:
36行92步
COPYFROM 23
COPYTO 20
COPYTO 21
first:
INBOX
JUMPZ second
COPYTO [23]
BUMPUP 23
JUMP first
second:
COPYTO [23]
input:
INBOX
JUMPZ replace0
COPYTO 19
COPYFROM [20]
JUMPZ out
SUB 19
JUMPN out
COPYFROM 19
SUB [20]
JUMPN replace
BUMPUP 20
JUMP input
replace:
COPYFROM 19
COPYTO [20]
continue:
BUMPUP 20
INBOX
JUMPZ out2
COPYTO [20]
JUMP continue
replace0:
COPYTO [20]
JUMP out
out2:
COPYTO [20]
out:
COPYFROM [21]
JUMPZ end
OUTBOX
BUMPUP 21
JUMP out
end:
第一次pass 47行118步
COPYFROM 23
COPYTO 20
COPYTO 21
COPYFROM 24
COPYTO 22
first:
INBOX
JUMPZ second
COPYTO [23]
BUMPUP 23
JUMP first
second:
INBOX
JUMPZ compare
COPYTO [24]
BUMPUP 24
JUMP second
compare:
COPYFROM [22]
SUB [21]
JUMPN o2
COPYFROM [21]
SUB [22]
JUMPN o1
BUMPUP 20
BUMPUP 21
SUB 23
JUMPZ o1
BUMPUP 22
SUB 24
JUMPZ o2
JUMP compare
o1:
COPYFROM 21
SUB 20
COPYTO 21
o11:
SUB 23
JUMPZ end
COPYFROM [21]
OUTBOX
BUMPUP 21
JUMP o11
o2:
COPYFROM 22
SUB 20
COPYTO 22
o22:
SUB 24
JUMPZ end
COPYFROM [22]
OUTBOX
BUMPUP 22
JUMP o22
end:
发表回复