例如,如果数组就像 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 ?
          ^

因此,光标被移动到任意长度的数据后面,但是将数据一个单元格移向了左侧。

免责声明 代码中可能存在错误,但是这个想法本身应该很清楚。只要代码与示例不匹配,就可以随意纠正。

其他提示

这可能太明显了,但是您可以通过 [>] 或者 [<]. 。因此,您可以在数据单元格(包装Peano)中添加1个,然后在输出之前减去1([-.>])?

可悲的是,这可能无法提供足够的控制。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top