当前位置: 首页 > 知识库问答 >
问题:

如何使用Brainfuck中的循环打印1到10的数字?有可能吗?

巴学潞
2023-03-14

如何在Brainfuck中使用循环打印1到10的数字?这可能吗?
我正在寻找这个问题的解决方案

共有3个答案

易星纬
2023-03-14
++++++++++++++++++++++++++++++++++++++++++++++++ Let address 0 be the digit we want to print, starting with '0'
>++++++++++ Let address 1 be our newline character
>+++++++++ Let address 2 be our counter, starting at 9 
[
  - Decrement the counter
  <<+. Increment the digit we want to print and print it
  >. Print the newline
  > Make sure we're at the counter again before we loop back
]

<< Move back to address 0
--------. Make address 0 '1'
-. Make address 0 '0'

现场演示

鲜于念
2023-03-14

TL;博士

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

在网上试试吧!

结束TL;博士

为了在BrainF**k中编程,假装每个程序(即使是简单的程序)都需要从布局开始。

这方面的伪代码如下所示:

Generate the character '0'
Move left and generate '\n'
Move left and generate the counter (10 numbers in this case)
Loop: Get back to the character '0', print it, increment it to '1', go to the newline, print it, go to the counter, and decrement it. End it when the counter is 0
Generate '1' and print it
Generate '0' and print it

然而,最后两个步骤可以简化为:

Go back to the digit '9'
Decrement it until '1' and print
Decrement it until '0' and print

这样可以节省大量时间和 字节 字符。

要生成字符“0”,需要生成整数48(因为它是ASCII值)。要做到这一点,你可以去Esolangs的BF常量。查找数字48,我们发现 -[

到目前为止,我们的程序是-[

接下来,向左移动并生成\n(换行符)。我们可以使用

到目前为止,我们的程序-[

然后,向左移动并生成计数器。我们希望计数器为10,以打印从0到9的数字<代码>

到目前为止,我们的程序是-[

之后,启动循环[。转到'0'

到目前为止,我们的程序是-[

最后,转到'9'

程序完成了。

丁志勇
2023-03-14
+++++++++++++++++++++++++++++++++++++++++++++++++  Cell 0 to '1'
>++++++++++  cell 1 to '\n'
>+++++++++  cell 2 to 9 as counter
[  Print numbers 1 to 9
<<  Data pointer to cell 0
.+  Print and increment cell 0
>.  Data pointer to cell 1 and print the newline
>-  Data pointer to cell 2 and decrement counter
]  Loop till counter is 0
+++++++++  Set cell 2 to 9
[  Set cell 0 to '1'
<<-  Data pointer to cell 0 and decrement
>>-  Data pointer to cell 2 and decrement counter
]  Loop till counter is 0
<<.  Data pointer to cell 0 and print '1'
-.   Decrement cell 0 and print '0'
>.   Data pointer to cell 1 and print newline
+++++++++++++++++++++++++++++++++++++++++++++++++>
++++++++++>
+++++++++[<<.+>.>-]
+++++++++[<<->>-]
<<.-.>.
1
2
3
4
5
6
7
8
9
10

Brainf**k print 1 to 10
Brainf**k Visualizer

 类似资料:
  • 我用C写了一个brainfuck解释器。这个程序有链表的实现,链表存储brainfuck指令,堆栈存储brainfuck的“[”指令。 我认为我的程序并不是在所有情况下都支持嵌套循环。这很荒谬,但这个brainfuck程序可以正确地使用我的Interpeter并打印“Hello World!”到stdout: 但该程序无法正常工作,并将内存错误打印到标准输出: 这两个例子适用于在线中断器-http

  • 我是多线程的新手,我有一个问题,要在Java中使用10个线程打印1到100,并使用以下约束。 > 线程 应打印: 1, 11, 21, 31, ...91 < code>t2应打印: 2, 12, 22, 32, ... 92 同样地 < code>t10应打印: 10, 20, 30, ... 100 最终输出应为: 1 2 3 ..100 我已经尝试过了,但它在所有10个线程中都抛出了以下异常

  • 是否有一种方法可以打印所有可能的选择1到x,其中没有重复的数字和可变数量的数字? 所以基本上如果x=10,n(列表的长度)=5 到目前为止我已经试过了,但不容易改变

  • 问题内容: 我正在画一个国际象棋骑士的可能路径,一种情况是这样的: 现在,而不是1和2,我想构造一个循环来尝试组合: 但是我不知道如果没有必要怎么做。至少有一种聪明的方法吗? 问题答案: 您可以创建一个Vector类或类似的类(或使用任何类似Pair的类型),用您的值填充列表并对其进行迭代(无需过多考虑性能): 如果您确实想节省一些行(您是在打高尔夫球吗?),则可以使用循环创建实例,但这并不能真正

  • 当我运行下面的程序时,没有输出,程序也不会退出。它应该打印素数。程序需要使用while循环运行。哪里出了问题?

  • 我们可以用这里所述的算法求有向图中的圈数。我需要理解算法。 (1)最后那句话到底有什么用处?对algo的工作原理进行简短的描述会很有帮助。由于算法基本上是统计从一个节点返回到同一节点的周期数,所以我们可以使用另一个数组,称之为v,并做以下技巧: (2)我不能实现我刚才写的算法。这是主要的问题,但我认为我需要理解上面的(1)来理解打印所有循环的代码。 我了解到互联网上有算法,我正在尝试使用这个算法。