例如,如果数组类似于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 ...,如何在移动指针后不更改s o m e d a t a 4 9 9 9 ...的情况下按n移动指针?
发布于 2011-06-16 20:37:27
不错的挑战。我喜欢这门语言:)
我的解决方案是(我认为)有点正确,它将光标移动到长度为n的数据之后而不改变它,但是它将数据向左移动两个单元格(或者一个,如果满足某些条件)。这是代码,希望你会觉得有用。
要求是:
and
代码如下:
>[<->+]<[>>[-<+>]<<[->>+<<]>[-<+>]>[-<+>]<-]示例:一个数组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 ?
^因此,光标移动到任意长度的数据后面,但是将数据左移一个单元格。
免责声明代码中可能有错误,但想法本身应该是清晰的。当代码与示例不匹配时,请随时更正代码。
发布于 2011-08-12 15:40:48
这可能太明显了,但您可以使用[>]或[<]将指针滑动到下一个零。那么,您是否可以在数据单元格中添加1(打包一个peano),然后在输出之前减去1 ([-.>])?
遗憾的是,这可能不能提供足够好的控制。
https://stackoverflow.com/questions/6307730
复制相似问题