求函数的结果

发布于 2024-11-07 07:15:11 字数 290 浏览 4 评论 0原文

有一个未知函数的代码:(

function Magic(number)
    r = number mod 2
    print r
    if number > 1
        Magic(number / 2)

用伪代码编写)

问题是:应该传递什么整数才能收到以下答案

0 1 1 0 0 1

主要问题是我无法弄清楚 mod 在伪代码中是如何工作的。 应 5,5 mod 3 = 2.5 或 2

There is a code of an unknown function:

function Magic(number)
    r = number mod 2
    print r
    if number > 1
        Magic(number / 2)

(written in pseudo-code)

The question is: what integer number should be passed in order to receive the following answer

0 1 1 0 0 1

The main problem is that I can't figure out how mod is working in pseudocode.
Should 5,5 mod 3 = 2.5 or 2

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

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

发布评论

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

评论(3

嗼ふ静 2024-11-14 07:15:11

除法和取模运算都应该只接受和输出整数。 “5.5 mod 3”fe 没有任何意义。 11 / 2(整数除法)将返回 5,而不是 5.5。

下面是一个实现伪代码的 PHP 程序:

<?php

function Magic($number) {
    $r = $number % 2;
    echo $r . ' ';

    if ($number > 1) Magic($number / 2);
}

for ($i = 16; $i < 34; ++$i) {
    echo "($i: ";

    Magic($i);
    echo ") ";
}

echo "\n";

输出结果:

(16: 0 0 0 0 1 ) (17: 1 0 0 0 1 0 ) (18: 0 1 0 0 1 0 ) (19: 1 1 0 0 1 0 ) (20: 0 0 1 0 1 0 ) (21: 1 0 1 0 1 0 ) (22: 0 1 1 0 1 0 ) (23: 1 1 1 0 1 0 ) (24: 0 0 0 1 1 0 ) (25: 1 0 0 1 1 0 ) (26: 0 1 0 1 1 0 ) (27: 1 1 0 1 1 0 ) (28: 0 0 1 1 1 0 ) (29: 1 0 1 1 1 0 ) (30: 0 1 1 1 1 0 ) (31: 1 1 1 1 1 0 ) (32: 0 0 0 0 0 1 ) (33: 1 0 0 0 0 1 0 )

显示 6 位结果 (x: 0 1 1 0 0 1 ) 对于任何整数 x 都是不可能的code> (因为输出字符串单调增长)。然而,Magic(38) 是 0 1 1 0 0 1 0 — 第一个 7 位结果带有所需的字符串,但也有一个尾随零。

至于负整数值,唯一可能的 2 个输出是“0”和“-1”。

Both division and mod operations are only supposed to accept and output integer numbers here. "5.5 mod 3" f.e. doesn't really make any sense. And 11 / 2 (integer division) will return 5, not 5.5.

Here's a PHP program that implements your pseudo code:

<?php

function Magic($number) {
    $r = $number % 2;
    echo $r . ' ';

    if ($number > 1) Magic($number / 2);
}

for ($i = 16; $i < 34; ++$i) {
    echo "($i: ";

    Magic($i);
    echo ") ";
}

echo "\n";

Results in output:

(16: 0 0 0 0 1 ) (17: 1 0 0 0 1 0 ) (18: 0 1 0 0 1 0 ) (19: 1 1 0 0 1 0 ) (20: 0 0 1 0 1 0 ) (21: 1 0 1 0 1 0 ) (22: 0 1 1 0 1 0 ) (23: 1 1 1 0 1 0 ) (24: 0 0 0 1 1 0 ) (25: 1 0 0 1 1 0 ) (26: 0 1 0 1 1 0 ) (27: 1 1 0 1 1 0 ) (28: 0 0 1 1 1 0 ) (29: 1 0 1 1 1 0 ) (30: 0 1 1 1 1 0 ) (31: 1 1 1 1 1 0 ) (32: 0 0 0 0 0 1 ) (33: 1 0 0 0 0 1 0 )

Which shows that the 6-digit result (x: 0 1 1 0 0 1 ) is impossible for any integer x (because of the monotonic growth of the output string). However, Magic(38) is 0 1 1 0 0 1 0 — the first 7-digit result with your required string, but also having a trailing zero.

As for the negative integer values, the only 2 outputs possible are "0 ", and "-1 ".

空气里的味道 2024-11-14 07:15:11

首先,这是解决这个问题的可执行Python代码。

def Magic(number):
r = number % 2
print r
if number > 1:
    Magic(number / 2)

Magic(15)       

然而,这个问题的模式是该函数返回给定输入数字的 REVERSE 二进制数字。因此,在这种情况下,最简单的解决方案是取 0 1 1 0 0 1,将其反转为 100110,并计算该二进制数的值,即 32 + 4 + 2 = 38。使用这种方法,您可以计算任何给定输入所需的数量或预期输出。

First of all, here is executable python code for this problem.

def Magic(number):
r = number % 2
print r
if number > 1:
    Magic(number / 2)

Magic(15)       

However, the pattern in this problem is that the function is returning the REVERSE binary number for the given input number. So, in this instance, the easiest solution would be to take 0 1 1 0 0 1, reverse it to 100110, and calculate the value of that binary number, which is 32 + 4 + 2 = 38. Using this methodology, you can calculate the required number or the expected output for any given input.

余生一个溪 2024-11-14 07:15:11

mod 给出整数除法的余数。举几个例子:

  • 1 mod 2 = 1(因为整数除法中 1 / 2 = 0)
  • 2 mod 2 = 0(2 / 2 = 1,无余数)
  • 6 mod 3 = 0(6 / 3 = 2,无余数)
  • 8 mod 3 = 2

小数部分可能应该被省略。在大多数语言中,它取决于数字的数据类型(如果两个操作数都是整数,某些语言会进行整数除法,这可能也是您应该做的,所以 5/2 = 2)。


至于你的第一个问题(剧透警告!,请在阅读本文之前自己尝试一下!),从末尾开始,每一步乘以 2。如果该步骤中的数字应为 1,则还要添加 1。

因此,最后一步是 1. 从 1 开始:

  • 1
  • 1*2 = 2
  • 2*2 = 4
  • 4*2+1 = 9

等等。我可以给你正确的答案,但我认为你自己尝试一下会更好;-)

mod gives you the remainder of a integer division. A few examples:

  • 1 mod 2 = 1 (since 1 / 2 = 0 in integer division)
  • 2 mod 2 = 0 (2 / 2 = 1, no remainder)
  • 6 mod 3 = 0 (6 / 3 = 2, no remainder)
  • 8 mod 3 = 2

The fraction part should probably be left out. in most languages it depends on the datatype of the number (some languages do integer division if both operands are integers, and this is probably what you should do too, so 5/2 = 2).


As to your first question (spoiler alert!, try it yourself before reading this!), begin from the end and multiply by two in every step. Also add 1 if the number should be 1 in that step.

So, the last step is a 1. Begin with 1:

  • 1
  • 1*2 = 2
  • 2*2 = 4
  • 4*2+1 = 9

and so on. I could give you the correct answer but I think it's better if you try it yourself ;-)

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