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
答案 0 :(得分:5)
这是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
1,000,000,000,000,000,000,000,000,000
您发布的代码问题是:
你正在设置你的PHP代码中的arr [i]而不是p(我将包含在这里,所以我的答案在你的问题编辑之后仍然有意义: - )。< / 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;
答案 1 :(得分:2)
我将程序输入TI-89并获得了1e27的p值的答案。
t(a)
Func
Local i,j,p,tmp
a[1]->p
For i,2,dim(a)
p->tmp
For j,1,a[i]
p*tmp->p
EndFor
EndFor
Return p
EndFunc
t({10,2,2,2}) 1.E27
答案 2 :(得分:1)
不是((10 ^ 3)^ 4)^ 5 = 10 ^ 60?
答案 3 :(得分:1)
似乎是一个计算函数
(((a_1^(a_2+1))^(a_3+1))^(a_4+1)...
因此得到((10 ^ 3)^ 3)^ 3 = 10 ^(3 ^ 3)= 10 ^ 27
答案 4 :(得分:1)
10 ^ 7的计算出错,见下文。正确答案是10 ^ 27 {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=p*temp, p=10^3 and temp=10^3, hence p=10^3 * 10^3.
p = 10^4 * 10 = 10^5 -- Similarly for other steps.
i = 4 to 4
temp = 10^5
j = 1 to 2
p = 10^5 * 10 = 10^6
p = 10^6 * 10 = 10^7
答案 5 :(得分:0)
人们称之为Python“可执行伪代码”是有原因的:
>>> def doSomething(*args):
... args = list(args);
... p = args.pop(0)
... for i in range(len(args)):
... temp = p
... for j in range(args[i]):
... p *= temp
... return p
...
>>> print doSomething(10,2,2,2)
1000000000000000000000000000
答案 6 :(得分:0)
#include <stdio.h>
double DoSomething(double array[], int count)
{
double p, temp;
int i, j;
p = array[0];
for(i=1;i<count;i++)
{
temp = p;
for(j=0; j<array[i];j++)
{
printf("p=%g, temp=%g\n", p, temp); /* useful to see what's going on */
p = p * temp;
}
}
return p; /* this isn't specified, but I assume it's the procedure output */
}
double array[4] = {10.0,2.0,2.0,2.0};
int main(void)
{
printf("%g\n", DoSomething(array, 4));
return 0;
}
而且,正如其他人所指出的那样,10e27。请注意,上面的伪代码非常详细 - 可以通过多种方式进行简化。
我使用了Tiny C Compiler - 非常小巧,轻巧,易于使用,非常适合这样的简单。
- 亚当