-2

我有任何挑战。我必须编写brainfuck-code。

对于给定的数字 n 指定它的最后一个数字。

入口

输入将仅包含一行,其中只有一个整数 n ( 1 < = n < = 2,000,000,000 ),后跟换行符 '\n' (ASCII 10)。

出口

在输出上必须找到一个整数,表示 n 的最后一位。

示例 I 入口:32 出口:2

例二:入口:231231132 出口:2

这是我尝试过的,但没有奏效:

+[>,]<.>++++++++++.
4

4 回答 4

2

不,对不起,真正的答案是

,[>,]<.

因为你的答案太过分了;)

于 2016-01-28T19:53:24.510 回答
2

最后一个输入是换行符。所以你必须回到两个记忆位置才能得到数字的最后一位。也许你不必返回换行符,所以代码是

,[>,]<<.
于 2015-07-07T11:43:19.637 回答
1

Your issue is that a loop continues for forever until at the end of the loop the cell the pointer is currently on in equal to 0. Your code never prints in the loop, and never subtracts, so your loop will never end, and all that your code does is take an ASCII character as input, move one forward, take an ASCII character as input, and so on. All of your code after the end of the loop is useless, because that your loop will never end.

于 2020-11-05T19:40:22.290 回答
1

根据解释器的不同,您可能必须自己转义返回键。考虑到返回键是ASCII: 10,您的代码应如下所示:

>,----- -----[+++++ +++++>,----- -----]<.

分解:

>               | //first operation (just in case your interpreter does not
                    support a negative pointer index)
,----- -----    | //first entry if it's a return; you don't even get in the loop
[                
    +++++ +++++ | //if the value was not ASCII 10; you want the original value back
    >,          | //every next entry
    ----- ----- | //check again for the the return, 
                    you exit the loop only if the last entered value is 10
]
<.              | //your current pointer is 0; you go back to the last valid entry
                    and you display it
于 2017-09-22T18:25:11.743 回答