Brainfuck语言基础
Prominence · · 算法·理论
Brainfuck 语法
:::info[官方解释]
|符号|解释|
|:--:|:--|
|>|increment the data pointer (to point to the next cell to the right).
|<|decrement the data pointer (to point to the next cell to the left).
|+|increment (increase by one) the byte at the data pointer.
|-|decrement (decrease by one) the byte at the data pointer.
|.|output the byte at the data pointer.
|,|accept one byte of input, storing its value in the byte at the data pointer.
|[|if the byte at the data pointer is zero, then instead of moving the instruction pointer forward to the next command, jump it forward to the command after the matching ] command.
|]|if the byte at the data pointer is nonzero, then instead of moving the instruction pointer forward to the next command, jump it back to the command after the matching [ command.
|!|if the exclaim box is checked, allows the interpreter to use all characters to the right of the ! as program input.
::::
::::info[洛谷翻译]
这种语言的运行机制十分简单,只有一个大小为
这种语言的字符集也是十分的简单,只有+ - , . > < []。
| 字符 | 意义 |
|---|---|
< |
指针所指向的内存地址减一。 |
> |
指针所指向的内存地址加一。 |
+ |
指针所指向的内存里面的数值加一。 |
- |
指针所指向的内存里面的数值减一。 |
. |
输出当前指针所指向的内存里面的数值(以字符形式输出)。 |
, |
将读入缓冲区中的一个字节送入当前指针指向的内存里面。如果读入缓冲区为空则送入 |
[ |
当前指针指向的内存里面的数值不为 ] 之间的语句,直到回到 [ 时当前指针指向的内存中的数值为 |
] |
如上。 |
| 字符在内存中的数值为字符所对应的 ASCII 码。 |
|---|
Brainfuck 入门
乘法
::::info[题目]
输出字符 0 (ASCII 48)
::::
::::success[一般做法]
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++++
+++++ +++
.
::::
::::success[推荐做法]
++++ ++++
[
- >
++++++ <
]
>.
::::info[解析]
先分析第一行 ++++ ++++:
第一轮循环:
我们可以注意到,每一轮循环都会使第一个格子
| 然后输出第二格就好了。 |
|---|
复制元素
::::info[题目] 输入一个字符,然后复制一份。
例如输入 A (ASCII 67):
Start: |67|0|0|0 |:--:|:--:|:--:|:--:
End: |67|67|0|0 |:--:|:--:|:--:|:--: ::::
::::success Step 1: |67|0|0|0 |:--:|:--:|:--:|:--: |+|||
Step 2: |0|67|67|0 |:--:|:--:|:--:|:--: |||+|
Step 3: |67|67|0|0 |:--:|:--:|:--:|:--: |+|||
,
[->+>+<<]>>
[-<<+>>]<<
::::
Hello World
接下来就尝试输出 Hello World 吧!
::::info 这里给出 ASCII 码(SPACE=32):
| H | e | l | l | o |
|---|---|---|---|---|
| 72 | 101 | 108 | 108 | 111 |
| W | o | r | l | d |
|---|---|---|---|---|
| 87 | 111 | 114 | 108 | 100 |
::::
::::success
+++++ +++++
[
> +++++ ++ Make 70
> +++++ ++++ Make 90
> +++++ +++++ Make 100
> +++++ +++++ + Make 110
> +++ Make 30
<<<<< -
]
>++. Output 'H'
>>+. Output 'e'
>--. Output 'l'
. Output 'l'
+++. Output 'o'
>++. Output ' '
<<<---. Output 'W'
>>. Output 'o'
+++. Output 'r'
------. Output 'l'
<-. Output 'd'
<<<
::::
结语
如果你非常耐心地看完了入门部分,那么恭喜你已经掌握 Brainfuck 的基础了。接下来就需要你手算 ASCII 了。
请你完成以下练习:
- 公比为
2 的等比数列 - 公差为
1 的等差数列 - 前
10 项 Fibonacci 数列 - 输出以下文本:
This is an apple. I like apples. Apples are good for my health.
Brainfuck 提高
引言
请你熟背 ASCII 码表并且安装一个 Brainfuck 编译/解释器。
NOT GATE
::::info[题目]
输入一个字符,请你判断是否有输入。
也就是输入
::::info 情况一: |67|0|0|0 |:--:|:--:|:--:|:--: |+|||
| 67 | 1 | 0 | 0 |
|---|---|---|---|
| + |
| 67 | 0 | 0 | 0 |
|---|---|---|---|
| + |
情况二: |0|0|0|0 |:--:|:--:|:--:|:--: |+|||
| 0 | 1 | 0 | 0 |
|---|---|---|---|
| + |
| 0 | 1 | 0 | 0 |
|---|---|---|---|
| + |
::::
::::success
,
>[-]+< 加 1
[>->] 如果不为 0,则减 1
>[>>]<< 两种情况指针位置合并
::::
带余除法
::::info[题目]
给你