gpt4 book ai didi

brainfuck - 如何在 BrainFuck 中任意移动指针?

转载 作者:行者123 更新时间:2023-12-01 07:45:05 26 4
gpt4 key购买 nike

例如,如果数组像 0 0 0 0 ... 0 0[n]s o m e d a t a 4 9 9 9 9 9 9 8 3 7 ...,如何移动指针通过 n,在指针移动后不改变 s o m e d a t a 4 9 9 9 ...

最佳答案

不错的挑战。我喜欢这种语言:)

我的解决方案(我认为)有些正确,它将光标移动到长度为 n 的数据之后而不改变它,但是它将数据向左移动两个单元格(或一个,前提是确定条件满足)。下面是代码,希望对你有用。

要求是:

  • 数据左侧有一个空单元格
  • 指针指向那个空单元格并且
  • 数据本身没有空单元格。

代码如下:

>[<->+]<[>>[-<+>]<<[->>+<<]>[-<+>]>[-<+>]<-]

示例:一个数组7 9 6,所以3告诉光标移动多远。

0 3 7 9 6 ?   this line represents data; values are numbers in cells
^ this shows the pointer location

>[<->+]< gives

3 0 7 9 6 ?
^

now the loop; while the pointer is not zero
[

>>[-<+>] gives in first iteration
3 7 0 9 6 ?
^

<<[->>+<<] gives in first iteration
0 7 3 9 6 ?
^

>[-<+>] gives in first iteration
7 0 3 9 6 ?
^

>[-<+>] gives in first iteration
7 3 0 9 6 ?
^

<- decreases the loop pointer and gives in first iteration
7 2 0 9 6 ?
^

which means the loop can continue giving
7 2 9 0 6 ?
7 0 9 2 6 ?
7 9 0 2 6 ?
7 9 2 0 6 ?
7 9 1 0 6 ? after the second iteration and finally
^
7 9 6 0 0 ? after the last
^
] which is where the loop will end

现在,如果整个序列的左边多了一个空单元格,那么我们可以将数据向右移动一个单元格。

0 7 9 6 0 0 ?
^

<[[>+<-]<] gives
0 0 7 9 6 0 ?
^

>>[>] gives finally
0 0 7 9 6 0 ?
^

所以光标移动到任意长度的数据后面,但是将数据向左移动一个单元格。

免责声明 代码中可能有错误,但思路本身应该很清楚。只要代码与示例不匹配,请随时更正代码。

关于brainfuck - 如何在 BrainFuck 中任意移动指针?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6307730/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com