头脑风暴中的Divmod算法 [英] Divmod algorithm in brainfuck

查看:68
本文介绍了头脑风暴中的Divmod算法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释此代码吗?我了解它的作用,但我不了解它的作用.

Can somebody please explain this code to me? I understand what it does but I don't understand how it works.

# >n 0 d
[->+>-[>+>>]>[+[-<+>]>+>>]<<<<<<]
# >0 n d-n%d n%d n/d

推荐答案

发生了什么:

# >n 0 d

此行是一条注释行,告诉您操作之前的内存应该是什么样的.股息为 n ,除数为 d .根据代码,接下来的3个单元格也应为空,但假设您默认情况下为空,则此处将其忽略.

This line, is a comment line telling you what the memory should be like before the operation. Dividend as n, divisor as d. According to the code the next 3 cells should be empty as well, but it is ignored here, assuming you have it empty by default.

为了便于理解,我现在以25/4为例:

For easier understanding, I'll now use 25/4 as an example:

ptr 000 001 002 003 004 005 006
val 025 000 004 000 000 000 000


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

此行可以分为几部分,以便于观察,但是如果您只是使用它,那将是一个魔术循环:

This line can be broken into parts for easier observation, but if you're just using it it's a magic loop:

[->+>-

这部分减去红利,将其加回到下一个要保存的单元格中,并除以除数.现在的内存是:

This part minuses the dividend, adds it back on the next cell for preservation, and minuses the divisor. The memory now is:

ptr 000 001 002 003 004 005 006
val 024 001 003 000 000 000 000


[>+>>]

这会添加从除数中删除的一个,以便再次保留,因为我们需要它循环返回.

This adds the removed one from the divisor, for preserving again, since we need it to loop back.

ptr 000 001 002 003 004 005 006
val 024 001 003 001 000 000 000

然后,它向右移动2步到单元格 005 ,然后由于中间是> ,而又将 006 跳过了[+ [-< +>]>>>>>] ,因为该单元格为空,然后由于此行而返回到单元格000:

Then, it moves 2 steps rightwards to cell 005, then 006 because of a > in between, skipping the [+[-<+>]>+>>] since the cell is empty, then back to cell 000 because of this line:

<<<<<<

额外的移动很重要,因为为了使系统不再再次循环,我们需要移至空白处.迁移到 006 基本上是因为需要额外的> ,这是以后使用所必需的.

The extra movement is important because, in order for the system not to loop back again, we need to move to an empty space. moving to 006 is basically because of the extra >, which is required for later usage.

让我们跳过一些步骤,然后继续前进直至除数变为0.

Let's skip some steps, and move forward until the divisor becomes 0.

ptr 000 001 002 003 004 005 006
val 021 004 000 003 000 000 000

由于单元格2现在为空,它会跳过 [>>>>] ,然后移至单元格 003 .

It skips the [>+>>] since cell 2 is empty now, and then it moves to cell 003.

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

由于 003 具有值,因此必须运行此行.在值上添加一个使其完整循环,然后使用 [-&+; +] 将值移回到 002 .指针在 003 处结束,因此它将移至 004 ,并将该值加1表示一个完整的周期,并因此再向商数加1.它移到 006 ,然后又移回到 000 .

Since 003 has a value, it has to run this line. adding one to the value to make it an complete loop, then shift the value back to 002 with the [-<+>]. The pointer ends at 003, so it moves to 004 and to add the value by one to indicate a full cycle and thus one more to the quotient. It moves to 006 and back to 000.

重复整个过程,然后我们得到:

Repeat the whole thing, then we get:

ptr 000 001 002 003 004 005 006
val 000 025 003 001 006 000 000

就像最后一行

# >0 n d-n%d n%d n/d

as循环终止,因为 000 现在为空.现在,n已完全移至 001 002 003 ,显示了n完全为零时除数的循环过程. 004 显示除数循环的完整完成迭代.

as loop terminates because 000 is now empty. n is now fully shifted to the 001, 002 and 003 shows the loop process of the divisor when the n is fully zeroed. 004 shows the total completed iteration of the divisor loop.

这篇关于头脑风暴中的Divmod算法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆