这段伪代码的输出是什么?
procedure DoSomething(a_1, ... a_n)
p = a_1
for i = 2 to n
temp = p
for j = 1 to a_i
p = p * temp
DoSomething(10,2,2,2)
我们得到的结果好坏参半。 我们一个人得到了 10^7,另一个得到了 10^27。
我想我发现了我的错误...我每次都用 10 代替 p,而不是 temp 的新值。
编辑:这是我的工作:
{10, 2, 2, 2}
p = 10
i = 2 to 4
temp = p = 10
j = 1 to 2
p = 10 * 10 = 10^2
p = 10^2 * 10 = 10^3
i = 3 to 4
temp = 10^3
j = 1 to 2
p = 10^3 * 10 = 10^4
p = 10^4 * 10 = 10^5
i = 4 to 4
temp = 10^5
j = 1 to 2
p = 10^5 * 10 = 10^6
p = 10^6 * 10 = 10^7
10^7
procedure DoSomething(a_1, ... a_n)
p = a_1
for i = 2 to n
temp = p
for j = 1 to a_i
p = p * temp
DoSomething(10,2,2,2)
We are getting mixed results. One of us got 10^7, the other 10^27.
I Think I found my error... I keep substituting 10 for p every time, instead of the new value for temp.
EDIT: here's my work:
{10, 2, 2, 2}
p = 10
i = 2 to 4
temp = p = 10
j = 1 to 2
p = 10 * 10 = 10^2
p = 10^2 * 10 = 10^3
i = 3 to 4
temp = 10^3
j = 1 to 2
p = 10^3 * 10 = 10^4
p = 10^4 * 10 = 10^5
i = 4 to 4
temp = 10^5
j = 1 to 2
p = 10^5 * 10 = 10^6
p = 10^6 * 10 = 10^7
10^7
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
它是 10^27,如这段 python 代码所示:
1,000,000,000,000,000,000,000,000,000
您发布的代码的问题是:
您在 PHP 代码中将 temp 设置为 arr[i],而不是 p(我将在此处包含该代码,因此在您将其从问题中编辑出来后,我的答案仍然有意义:-)。
It's 10^27 as shown by this bit of python code:
1,000,000,000,000,000,000,000,000,000
The problems with your code as posted are:
You're setting temp to arr[i], not p, in your PHP code (which I'll include here so my answer still makes sense after you edited it out of your question :-).
我将程序输入 TI-89 并得到 p 值的答案 1e27。
I entered the program into my TI-89 and got an answer of 1e27 for the value of p.
人们将 Python 称为“可执行伪代码”是有原因的:
There's a reason folks have called Python "executable pseudocode":
不是 ((10^3)^4)^5 = 10^60 吗?
Isn't it ((10^3)^4)^5 = 10 ^ 60 ?
似乎是一个函数来计算
因此我们得到 ((10^3)^3)^3 = 10^(3^3) = 10^27
Seems to be a function to calculate
Thus we get ((10^3)^3)^3 = 10^(3^3) = 10^27
您的 10^7 计算存在错误,请参见下文。 正确答案是 10^27
{10,2,2,2}
There is an error in your computation for 10^7, See below. The correct answer is 10^27
{10, 2, 2, 2}
在 C 中:
并且,正如其他人指出的那样,10e27。 请注意,上面的伪代码非常冗长 - 它可以通过多种方式简化。
我使用了 Tiny C 编译器 - 非常小、轻量级且易于用于此类简单的操作。
-亚当
In C:
And, as others have indicated, 10e27. Note that the above is very verbose from your pseudo code - it could be simplified in many ways.
I used the Tiny C Compiler - very small, lightweight, and easy to use for simple stuff like this.
-Adam