Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Algorithm 模糊代码:最后一位_Algorithm_Brainfuck - Fatal编程技术网

Algorithm 模糊代码:最后一位

Algorithm 模糊代码:最后一位,algorithm,brainfuck,Algorithm,Brainfuck,我很难用brainfuck语言编写模糊代码来完成以下任务: 对于给定的数字n,输出其最后一位 输入 输入将只包含一行,其中只有一个整数n(1+>+>++++++++++//写入10(换行符)并输入循环 [ -//减少10个计数器和一个复制的输入 ] ,-----------[+++++>,------]甚至这个:,------[-],------]

我很难用brainfuck语言编写模糊代码来完成以下任务:

对于给定的数字n,输出其最后一位

输入 输入将只包含一行,其中只有一个整数n(1<=n<=20000000000),后跟一个换行“\n”(ASCII 10)

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

例一 输入:32 产出:2

例二: 输入:231132 产出:2

这是我尝试过的,但没有成功:

+[>,]<.>++++++++++.
+[>,]++++++++++++。

问题在于,您告诉循环在输入为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.
+[//将[1]增加1并进入循环
,[//读取[1]的输入并进入另一个循环
>+>+>++++++++++//写入10(换行符)并输入循环
[
-//减少10个计数器和一个复制的输入
]
<返回(输入-10)单元格

]你能评论一下你现有的程序吗?我的代码读输入,写最后一位,写“\n”我的意思是,源代码注释。还有,你不应该在输入的换行处停止阅读吗?你有两个开始的括号,三个结束的括号,所以代码失败了。看起来我在注释中丢失了一个。现在应该工作了吗?我更喜欢这个版本<代码>>,-----------[+++++>,------]甚至这个:
,------[-],------]