Brainfuck 比较 2 个数字大于或小于

发布于 2024-11-10 11:48:51 字数 97 浏览 4 评论 0原文

如何比较两个不等式的数字? (大于或小于)

我想比较个位数 例如

1 2
5 3
9 2

等等。

How can I compare two numbers with an inequality? (greater than or less than)

I want to compare single digits
For example

1 2
5 3
9 2

etc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

怪我闹别瞎闹 2024-11-17 11:48:51

这是比较两个数字的最佳方法。为什么是因为,如果您足够聪明,您可以在更大的程序中使用相同的代码。它具有高度可移植性。

假设我们有两个数字a,b。
我们有两个块:if( a>=b )else
希望它足够了。

    0 1 0 a b 0

使数组像这样。并指向 (4) 即指向 a

    +>+<                   This is for managing if a=0 and b=0
    [->-[>]<<]             This is a magic loop. if a is the one which 
                           reaches 0 first (a<b),then pointer will be at(4).
                           Else it will be at (3)
    <[-  
         //       BLOCK (a>=b)
         //You are at (2) and do whatever you want and come back to (2).
         //Its a must
    ]
    <[-<
         //       BLOCK(a<b)
         //You are at (1) and do whatever you want and come back to (1).
         //Its a must
    ]

它不会影响下面的程序代码,因为两个代码块都将在 (1) 中结束 您可以假设指针将到达 ( 1)

如果复制代码,请删除文档。因为代码包含一些有效的 Brainfuck 符号,例如 < 。 , ETC。

This is the best way to compare two numbers.Why because, if you are intelligent enough, you can use the same code in bigger programs.It's highly portable.

Assume we have two numbers a,b.
we have two blocks : if( a>=b ) and else,
Hope its enough.

    0 1 0 a b 0

Make the array like this. And point to the (4) i.e. point to the a

    +>+<                   This is for managing if a=0 and b=0
    [->-[>]<<]             This is a magic loop. if a is the one which 
                           reaches 0 first (a<b),then pointer will be at(4).
                           Else it will be at (3)
    <[-  
         //       BLOCK (a>=b)
         //You are at (2) and do whatever you want and come back to (2).
         //Its a must
    ]
    <[-<
         //       BLOCK(a<b)
         //You are at (1) and do whatever you want and come back to (1).
         //Its a must
    ]

It will not affect the following program code as both the code blocks will end up in (1) You can do further coding assuming that pointer will reach (1)

Please remove the documentation if you copy the code. Because code contains some valid brainfuck symbols like < . , etc.

夜灵血窟げ 2024-11-17 11:48:51

一旦您知道两个数字之间的距离,您应该在同一个循环迭代中减少这两个数字,然后检查两个数字是否为零:您将了解哪个数字较小。

例如:(

+++++ > +++ < [->-< check is first is zero, then second]

这只是给你一个提示,你必须注意相等的数字和类似的问题。

Once you know which is the distance between the two numbers you should or decrement both of them in the same loop iteration and then check both for being zero: you will understand which one is the smaller.

Eg:

+++++ > +++ < [->-< check is first is zero, then second]

(this is just to give you a hint, you will have to take care about equal numbers and similar issues.

深居我梦 2024-11-17 11:48:51

我也在考虑这个问题,虽然我确信这不是最好的解决方案,但至少它可以回答哪个数字更大的问题 =)

程序要求两个字符,输出 '<'如果第一个较小,则“>”如果更大,则为“=”,如果相等则为“=”。输出一个字符后,程序会通过要求额外的输入而停止。

+>,>,<<[>-[>>>]<[>>-[>++++++++++[-> ;++++++<]>.,]++++++++++[->++++++<]>+.,]<-[> >>]<<[>>++++++++++[->++++++<]>++.,]<< <]

希望更清楚一些:

+                                   init (0) to 1
>,                                  read (1)
>,                                  read (2)
<<[                                 loop forever
  >-[>>>]                           decrement (1) going to (4) if (1) != 0
  <[                                goto (0) == 1 if (1) reached 0 (otherwise goto (3))
    >>-[>++++++++++[->++++++<]>.,]  decrement (2) printing lessthan if larger than 0
    ++++++++++[->++++++<]>+.,       if (2) == 0 print '='
  ]
  <-[>>>]                           decrement (2) going to (5) if (2) != 0
  <<[                               goto (0) == 1 if (2) reached 0 (otherwise goto (3))
    >>>++++++++++[->++++++<]>++.,   print largerthan since (2) reached 0 first
  ]
  <<<                               goto(0)
]

I was thinking about this too, and while I'm sure this isn't the best solution, at least it can answer the question of which number is larger =)

The program asks for two characters, outputs '<' if the first is smaller, '>' if it is larger, and '=' if they are equal. After outputting one char, the program halts by asking for additional input.

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

Hopefully somewhat clearer:

+                                   init (0) to 1
>,                                  read (1)
>,                                  read (2)
<<[                                 loop forever
  >-[>>>]                           decrement (1) going to (4) if (1) != 0
  <[                                goto (0) == 1 if (1) reached 0 (otherwise goto (3))
    >>-[>++++++++++[->++++++<]>.,]  decrement (2) printing lessthan if larger than 0
    ++++++++++[->++++++<]>+.,       if (2) == 0 print '='
  ]
  <-[>>>]                           decrement (2) going to (5) if (2) != 0
  <<[                               goto (0) == 1 if (2) reached 0 (otherwise goto (3))
    >>>++++++++++[->++++++<]>++.,   print largerthan since (2) reached 0 first
  ]
  <<<                               goto(0)
]
ゞ花落谁相伴 2024-11-17 11:48:51

我提出了一个解决方案,它返回一个布尔值,并且指针始终位于同一点。

这是它一开始的样子:

0 0 0 a b 0 0
      p

这是两个可能的输出:

0 0 0 0 0 1 0   #true
            p

0 0 0 0 0 0 0   #false
            p

代码:

 >>>>
    [                 # while cell != 0
      -               #   decrement a
      [               #   if a != 0
        >-            #     decrement b 
        [             #     if b != 0
          <           #       go left
          <-<         #       undo the finally-block;
        ]             #     finally-block
        <[-]>         #       clear a
        >+>           #       res = 1; move to end-position
        <<<           #       undo the finally-block
      ]               #   finally-block
      >[-]>>          #     clear b; res = 0; move to end-position
    ]                 #

缩小版本:

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

I made a solution, that gives you back a boolean and the pointer always at the same point.

This is how it looks like at the beginning:

0 0 0 a b 0 0
      p

And these are the two possible outputs:

0 0 0 0 0 1 0   #true
            p

0 0 0 0 0 0 0   #false
            p

The code:

 >>>>
    [                 # while cell != 0
      -               #   decrement a
      [               #   if a != 0
        >-            #     decrement b 
        [             #     if b != 0
          <           #       go left
          <-<         #       undo the finally-block;
        ]             #     finally-block
        <[-]>         #       clear a
        >+>           #       res = 1; move to end-position
        <<<           #       undo the finally-block
      ]               #   finally-block
      >[-]>>          #     clear b; res = 0; move to end-position
    ]                 #

minified version:

 >>>>[-[>-[< <-<]<[-]>>+><<<]>[-]>>]
旧梦荧光笔 2024-11-17 11:48:51

给定两个数字 A 和 B,如果 A 大于 B,则以下代码将打印 A;如果 B 大于 A,则打印 B;如果两者相等,则以下代码将打印 C。

>>>>>>>>>++++++[>+++++++++++<-]>[> +>+>+<<<-]>+>->
<<<<<<<<<<<<<<,>,<
[->-<[>]<<]>>>[>>]>>>>>>>>。

Given two numbers A and B, the following code will print A if A is greater than B, B if B is greater than A and C if both are equal.

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

ˉ厌 2024-11-17 11:48:51

BF里不存在这样的东西。 BF 中的 >< 分别将指针向右和向左移动。

No such thing exists in BF. The > and < in BF move the pointer to the right and to the left, respectively.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文