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)
Run Code Online (Sandbox Code Playgroud)
我们的结果好坏参半.我们其中一人得到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
Run Code Online (Sandbox Code Playgroud)
10 ^ 7
这是python代码所示的10 ^ 27:
a = [10,2,2,2]
p = a[0]
for i in range(1,len(a)):
temp = p
for j in range(a[i]):
p *= temp
print p
Run Code Online (Sandbox Code Playgroud)
1,000,000,000,000,000,000,000,000,000
您发布的代码问题包括:
你在你的PHP代码中设置了arr [i]而不是p的临时值(我将在这里包含所以我的答案在您编辑出问题之后仍然有意义:-).
$arr = array(10, 2, 2, 2);
$p = $arr[0];
$temp = 0;
for($i = 1; $i <= 3; $i++)
{
$temp = $arr[$i];
for($j = 0; $j <= $arr[$i]; $j++)
{
$p = $p * $temp;
}
}
echo $p;
Run Code Online (Sandbox Code Playgroud)