深奥的编程和我的分析
esoteric programming and my analysis
最近遇到一个关于深奥的编程语言的问题。
有那个语言的工具。
> - increases the data pointer (so it points to the next cell in the array);
< - decreases the data pointer;
+ - increments the value in the current memory cell (i.e. one pointed by data pointer);
- - decrements the value in the current memory cell;
. - takes the value of current memory cell, converts it to character and prints to output;
, - takes one character from input and puts its ASCII code to the current memory cell.
[ - peeks at current cell, if the value here is non-zero, then simply proceeds to the next instruction, but if the value is 0, then searches forward for corresponding ] and sets code pointer immediately after this (i.e. skips the block within brackets);
] - moves code pointer back, to corresponding [.
: - takes the value of current memory cell and prints to output as a decimal integer;
; - takes next decimal integer (probably, consisting of several digits) from input and puts it to the current cell.
所以我必须用这种语言编写一个程序,输入两个数字 a 和 b 并将它们放在 cell0 和 cell1 中,并输出这两个数字的和。还有一个额外的要求(我遇到了麻烦)是在这个过程之后应该有 3 个单元格,单元格 0 包含 a,单元格 1 包含 b,单元格 2 包含 a+b。
这是我的分析:我认为找到将总和放入 cell3 并打印它的方法很容易,只需执行 ;>;<[->>+]>[->+]>:
。但是这样处理后,cell0和cell1都将保持0而不是a和b。所以我试图找到一种方法来使用上面的工具来实现这一点,我意识到有了这个工具,它就像一块电池,我只能将能量从一个电池转移到另一个电池,但我永远不能将能量从一个电池复制到另一个.如果是这样,我在尝试保留 cell0 和 cell1 时永远无法得到总和。
感谢@user3386109 在我的问题下评论的信息。我注意到有一些方法可以作弊 "energy balance"。我们可以在循环中增加 2 个或更多单元格。所以我使用 5 个单元格并将第一个单元格和第二个单元格中的 a 和 b 转移到第四个和第五个单元格,同时进行求和运算。所以我的算法将是这样的:
;>; input two numbers a and b
<[->>+>+] if the first cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 4th cell until it's zero.
>[->+>>+] if the second cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 5th cell until it's zero.
then we transfer back value a and b from 4th and 5th cell to 1st and 2nd cell
>>[-<<<+]>[-<<<+]
<<: go back 3rd cell and print out.
最后我的代码是:
;>;<[->>+>+]>[->+>>+]>>[-<<<+]>[-<<<+]<<:
但是不对啊,查了好几遍也没找到bug。有人帮帮我吗?谢谢!!!
您使用的语言本质上是 Brainfuck(加上 ;
和 :
运算符,为方便起见,但它们不会真正影响语言的工作方式) .
正如 user3386109 在评论中指出的那样,诀窍是在将输入添加到结果时复制输入。您的解决方案几乎是正确的,但您似乎忘记了在每个循环中将内存指针移回。您的循环只是在每次迭代中进一步向右移动。除非你在做一些有趣的事情,你打算在循环中沿着磁带移动,否则你通常会希望 <
和 >
在每个循环中保持平衡。
这是您的问题的一种可能解决方案:
; Read a into cell 0
[->+>>+<<<] Add a to cells 1 and 3
; Read b into cell 0
[->>+>+<<<] Add b to cells 2 and 3
>>>: Print cell 3 (with cells 1 and 2 holding copies of a and b)
您可以(某种程度上)测试这个 here。这是一个标准的 Brainfuck 解释器,所以它不支持 ;
和 :
,但是如果我们添加字符 !
(33) 和 A
(65),我们得到b
(98).
有关如何在 Brainfuck 中解决问题的更多信息,我推荐我在顶部链接到的 esolangs 文章,以及您可以在该文章底部找到的许多链接。
最近遇到一个关于深奥的编程语言的问题。 有那个语言的工具。
> - increases the data pointer (so it points to the next cell in the array);
< - decreases the data pointer;
+ - increments the value in the current memory cell (i.e. one pointed by data pointer);
- - decrements the value in the current memory cell;
. - takes the value of current memory cell, converts it to character and prints to output;
, - takes one character from input and puts its ASCII code to the current memory cell.
[ - peeks at current cell, if the value here is non-zero, then simply proceeds to the next instruction, but if the value is 0, then searches forward for corresponding ] and sets code pointer immediately after this (i.e. skips the block within brackets);
] - moves code pointer back, to corresponding [.
: - takes the value of current memory cell and prints to output as a decimal integer;
; - takes next decimal integer (probably, consisting of several digits) from input and puts it to the current cell.
所以我必须用这种语言编写一个程序,输入两个数字 a 和 b 并将它们放在 cell0 和 cell1 中,并输出这两个数字的和。还有一个额外的要求(我遇到了麻烦)是在这个过程之后应该有 3 个单元格,单元格 0 包含 a,单元格 1 包含 b,单元格 2 包含 a+b。
这是我的分析:我认为找到将总和放入 cell3 并打印它的方法很容易,只需执行 ;>;<[->>+]>[->+]>:
。但是这样处理后,cell0和cell1都将保持0而不是a和b。所以我试图找到一种方法来使用上面的工具来实现这一点,我意识到有了这个工具,它就像一块电池,我只能将能量从一个电池转移到另一个电池,但我永远不能将能量从一个电池复制到另一个.如果是这样,我在尝试保留 cell0 和 cell1 时永远无法得到总和。
感谢@user3386109 在我的问题下评论的信息。我注意到有一些方法可以作弊 "energy balance"。我们可以在循环中增加 2 个或更多单元格。所以我使用 5 个单元格并将第一个单元格和第二个单元格中的 a 和 b 转移到第四个和第五个单元格,同时进行求和运算。所以我的算法将是这样的:
;>; input two numbers a and b
<[->>+>+] if the first cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 4th cell until it's zero.
>[->+>>+] if the second cell is not zero then we keep decrementing it and incrementing the number in 3rd cell and 5th cell until it's zero.
then we transfer back value a and b from 4th and 5th cell to 1st and 2nd cell
>>[-<<<+]>[-<<<+]
<<: go back 3rd cell and print out.
最后我的代码是:
;>;<[->>+>+]>[->+>>+]>>[-<<<+]>[-<<<+]<<:
但是不对啊,查了好几遍也没找到bug。有人帮帮我吗?谢谢!!!
您使用的语言本质上是 Brainfuck(加上 ;
和 :
运算符,为方便起见,但它们不会真正影响语言的工作方式) .
正如 user3386109 在评论中指出的那样,诀窍是在将输入添加到结果时复制输入。您的解决方案几乎是正确的,但您似乎忘记了在每个循环中将内存指针移回。您的循环只是在每次迭代中进一步向右移动。除非你在做一些有趣的事情,你打算在循环中沿着磁带移动,否则你通常会希望 <
和 >
在每个循环中保持平衡。
这是您的问题的一种可能解决方案:
; Read a into cell 0
[->+>>+<<<] Add a to cells 1 and 3
; Read b into cell 0
[->>+>+<<<] Add b to cells 2 and 3
>>>: Print cell 3 (with cells 1 and 2 holding copies of a and b)
您可以(某种程度上)测试这个 here。这是一个标准的 Brainfuck 解释器,所以它不支持 ;
和 :
,但是如果我们添加字符 !
(33) 和 A
(65),我们得到b
(98).
有关如何在 Brainfuck 中解决问题的更多信息,我推荐我在顶部链接到的 esolangs 文章,以及您可以在该文章底部找到的许多链接。