求函数的结果
有一个未知函数的代码:(
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除法和取模运算都应该只接受和输出整数。 “5.5 mod 3”fe 没有任何意义。 11 / 2(整数除法)将返回 5,而不是 5.5。
下面是一个实现伪代码的 PHP 程序:
输出结果:
显示 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:
Results in output:
Which shows that the 6-digit result
(x: 0 1 1 0 0 1 )
is impossible for any integerx
(because of the monotonic growth of the output string). However, Magic(38) is0 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 ".
首先,这是解决这个问题的可执行Python代码。
然而,这个问题的模式是该函数返回给定输入数字的 REVERSE 二进制数字。因此,在这种情况下,最简单的解决方案是取 0 1 1 0 0 1,将其反转为 100110,并计算该二进制数的值,即 32 + 4 + 2 = 38。使用这种方法,您可以计算任何给定输入所需的数量或预期输出。
First of all, here is executable python code for this problem.
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.
mod
给出整数除法的余数。举几个例子:小数部分可能应该被省略。在大多数语言中,它取决于数字的数据类型(如果两个操作数都是整数,某些语言会进行整数除法,这可能也是您应该做的,所以 5/2 = 2)。
至于你的第一个问题(剧透警告!,请在阅读本文之前自己尝试一下!),从末尾开始,每一步乘以 2。如果该步骤中的数字应为
1
,则还要添加 1。因此,最后一步是 1. 从
1
开始:等等。我可以给你正确的答案,但我认为你自己尝试一下会更好;-)
mod
gives you the remainder of a integer division. A few examples: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
:and so on. I could give you the correct answer but I think it's better if you try it yourself ;-)