-3

次のことを行うために、brainfuck 言語で難読化されたコードを作成するという課題があります。

与えられた数値 n について、その最後の桁を出力します。

入力

入力は、整数 n ( 1 < = n < = 2,000,000,000 ) が 1 つしかない 1 行だけで構成され、その後に改行 ' \ n' (ASCII 10) が続きます。

出力

出力では、n の最後の桁を表す整数を 1 つだけ見つけなければなりません。

例 入力: 32 出力: 2

例 II: 入力: 231231132 出力: 2

これは私が試したものですが、うまくいきませんでした:

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

1 に答える 1

3

問題は、入力が 0 のときにループを終了するように指示していることです。

入力は決して 0 ではありません。改行の ASCII は 10 なので、それを使用する必要があります。

このコードは機能するはずです。実際には、このコードは数値を指定してもまったく気にせず、最初に見つかった改行の前の最後の文字を返すだけです。

+[     // increment [1] by 1 and enter the loop
  ,[     // read input to [1] and enter another loop
     >+>+<<-     // copy the initial input twice whilst decrementing the original
  ]
  >>>++++++++++     // write 10 (for newline) and enter a loop
  [
    <->-     // decrement the 10 counter and one of the copied inputs
  ]
< step back to the (input - 10) cell
]<<<.     // if (input - 10 == 0) then you just read a carriage return! yay! Step back by three to the last stored input and print it out to the console.
于 2015-10-15T14:53:40.537 に答える