代码之家  ›  专栏  ›  技术社区  ›  TT.

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

  •  1
  • TT.  · 技术社区  · 17 年前
    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, 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

    7 回复  |  直到 14 年前
        1
  •  5
  •   paxdiablo    17 年前

    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代码中,您将temp设置为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;
      
        2
  •  2
  •   Adrian    17 年前

    我将程序输入到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
    
        3
  •  1
  •   Mike Kruger    17 年前

        4
  •  1
  •   Overflown    17 年前

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

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

        5
  •  1
  •   Charles Duffy    17 年前

    {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
    
        6
  •  0
  •   Charles Duffy    17 年前

    人们称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
    
        7
  •  0
  •   Adam Davis    17 年前

    在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 Compiler -非常小,重量轻,易于使用的简单的东西像这样。

    推荐文章