Algorithm 深奥编程与我的分析

Algorithm 深奥编程与我的分析,algorithm,brainfuck,esoteric-languages,Algorithm,Brainfuck,Esoteric Languages,最近我遇到了一个关于深奥编程语言的问题。 这种语言有很多工具 > - 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 t

最近我遇到了一个关于深奥编程语言的问题。 这种语言有很多工具

> - 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.
>-增加数据指针(使其指向数组中的下一个单元格);
<-减少数据指针;
+-增加当前存储单元中的值(即数据指针指向的值);
--递减当前存储单元中的值;
. - 获取当前存储单元的值,将其转换为字符并打印输出;
,-从输入中提取一个字符,并将其ASCII码放入当前存储单元。
[-查看当前单元格,如果此处的值非零,则直接进入下一条指令,但如果该值为0,则向前搜索相应的]并立即在此之后设置代码指针(即跳过括号内的块);
]-将代码指针移回对应的[。
:-取当前存储单元的值,以十进制整数打印输出;
;-从输入中获取下一个十进制整数(可能由几个数字组成),并将其放入当前单元格。
因此,我必须用这种语言编写一个程序,输入两个数字a和b,放入cell0和cell1,然后输出这两个数字的总和。还有一个额外的要求(我遇到了麻烦),就是在这个过程之后应该有3个单元格,cell0保持a,cell1保持b,Cell2保持a+b

以下是我的分析:我认为找到将总和放入单元格3并打印出来的方法很简单,只需执行
;>;>+]>[->+]>:
。然而,通过这种方式,在处理之后,cell0和cell1都将保持0,而不是a和b。所以我试图找到一种方法来使用上面的工具来实现这一点,我意识到有了这个工具,它就像一个电池,我只能将能量从一个电池转移到另一个,但我永远无法将能量从一个复制到另一个。如果是这样的话,当我试图保存cell0和cell1时,我永远无法得到总和

感谢@user3386109在我的问题下的评论。我注意到有一些方法可以欺骗“能量平衡”。我们可以在循环中增加2个或更多单元格。因此,我使用5个单元格,在进行求和运算时,将第一个单元格中的a和b以及第二个单元格中的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.
;>;输入两个数字a和b
>+>+]如果第一个单元格不是零,那么我们将继续递减它,并递增第三个单元格和第四个单元格中的数字,直到它为零。
>[->+>>+]如果第二个单元格不是零,那么我们将不断递减它,并递增第三个单元格和第五个单元格中的数字,直到它为零。
然后,我们将值a和b从第四和第五个单元格传回第一和第二个单元格

>>[-+]>>[-您使用的语言本质上是(为了方便起见,加上
运算符,但它们并不真正影响语言的工作方式)

正如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)
;将数据读入单元格0
[->+>>++:打印单元格3(单元格1和2保存a和b的副本)
你可以(某种程度上)测试这个。它是一个标准的Brainfuck解释器,所以它不支持
,但是如果我们添加字符
(33)和
a
(65),我们会得到
b
(98)


有关如何解决Brainfuck问题的更多信息,我推荐顶部链接的Esolags文章,以及底部的许多链接。

你可以使用额外的单元格吗?是的,它没有这个限制规则提示:
;[->+>+user3386109很棒的提示,thx!!!thx谢谢你的帮助,我想“[”自动返回到上一个单元格,我现在得到它!
;              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)