这段伪代码的输出是什么?

发布于 2024-07-13 03:10:56 字数 599 浏览 12 评论 0原文

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 技术交流群。

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

发布评论

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

评论(7

黎歌 2024-07-20 03:10:57

它是 10^27,如这段 python 代码所示:

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

您发布的代码的问题是:

  • 在您的 10^7 解决方案中,您总是乘以 10,而不是 temp (它增加到 p 的最终值)在 j 循环之后)。
  • 您在 PHP 代码中将 temp 设置为 arr[i],而不是 p(我将在此处包含该代码,因此在您将其从问题中编辑出来后,我的答案仍然有意义:-)。

    $arr = 数组(10, 2, 2, 2); 
      $p = $arr[0]; 
      $温度= 0; 
      for($i = 1; $i <= 3; $i++) 
      { 
          $temp = $arr[$i]; 
          for($j = 0; $j <= $arr[$i]; $j++) 
          { 
              $p = $p * $temp; 
          } 
      } 
      回声 $p; 
      

It's 10^27 as shown by this bit of python code:

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

The problems with your code as posted are:

  • in your 10^7 solution, you're always multiplying by 10, not temp (which is increased to the final value of p after the j loop).
  • 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 :-).

    $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;
    
为人所爱 2024-07-20 03:10:57

我将程序输入 TI-89 并得到 p 值的答案 1e27。

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

I entered the program into my TI-89 and got an answer of 1e27 for the value of 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
千年*琉璃梦 2024-07-20 03:10:57

人们将 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

There's a reason folks have called Python "executable pseudocode":

>>> 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
会傲 2024-07-20 03:10:57

不是 ((10^3)​​^4)^5 = 10^60 吗?

Isn't it ((10^3)^4)^5 = 10 ^ 60 ?

悲欢浪云 2024-07-20 03:10:57

似乎是一个函数来计算


(((a_1^(a_2+1))^(a_3+1))^(a_4+1)...

因此我们得到 ((10^3)​​^3)^3 = 10^(3^3) = 10^27

Seems to be a function to calculate


(((a_1^(a_2+1))^(a_3+1))^(a_4+1)...

Thus we get ((10^3)^3)^3 = 10^(3^3) = 10^27

云胡 2024-07-20 03:10:57

您的 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

There is an error in your computation for 10^7, See below. The correct answer is 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
月亮坠入山谷 2024-07-20 03:10:57

在 C 中:

#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 编译器 - 非常小、轻量级且易于用于此类简单的操作。

-亚当

In C:

#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;
}

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

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