嵌套循环结果

时间:2013-06-10 08:33:21

标签: loops for-loop pseudocode nested-loops

我真的不知道如何找出嵌套循环的结果。 例如,在下面的伪代码中,我无法理清在执行结束时将给出的内容。如果有人给我一个简单的解决方案,我会很高兴。

r <- 0
for i <- 1 to n do 
  for j <- 1 to i do
    for k <- j to i+j do
      r <- r + 1
return r

问题是:

代码的结果是什么,并根据r给出结果n

我写的但每次我都感到困惑。

3 个答案:

答案 0 :(得分:2)

在您的伪代码中,最内层循环k <- j to i+j可以写为k <- 0 to i这是删除j )。因此,您的代码可以简化如下:

r <- 0
for i <- 1 to n do 
    for j <- 1 to i do
      for k <- 0 to i do   // notice here `j` removed
        r <- r + 1
return r

基于这个伪代码,我编写了一个C程序(如下所示)来生成N = 1到10的序列。(您最初将问题标记为,但我正在编写代码,因为你想要的是独立于语言约束)

#include<stdio.h>
int main(){
  int i =0, k =0, j =0, n =0;
  int N =0; 
  int r =0;
  N =10;
  for (n=1; n <= N; n++){
  // unindented code here
  r =0;
  for (i=1; i<=n; i++)
      for (j=1; j<=i; j++)
          for (k=0; k<=i; k++)
              r++;    
  printf("\n N=%d  result = %d",n, r); 
  }
  printf("\n");
}

此程序的输出类似于:

 $ ./a.out     
 N=1  result = 2
 N=2  result = 8
 N=3  result = 20
 N=4  result = 40
 N=5  result = 70
 N=6  result = 112
 N=7  result = 168
 N=8  result = 240
 N=9  result = 330
 N=10  result = 440

然后,尝试用一些图表来探索它是如何工作的?

N=1的执行树:

1<=i<=1,              (i=1)
                        |
1<=j<=i,              (j=1)
                      /   \
0<=k<=i,          (K=0)    (K=1)
                   |        |  
r=0                r++      r++    => r = 2
                 ( 1   +    1 )

那是(1*2) = 2

N=2的树:

1<=i<=2,         (i=1)-----------------------(i=2)
                  |                 |---------|------|
1<=j<=i,        (j=1)           (j=1)              (j=2)
                /   \          /  |  \            /  |  \
0<=k<=i,    (K=0)    (K=1)  (K=0)(K=1)(k=2)    (K=0)(K=1)(k=2)
              |        |      |     |    |       |     |    |
r=0          r++      r++   r++   r++  r++     r++   r++  r++    => 8
            --------------  ---------------------------------
            ( 1   +    1)   (     3         +      3        )

那是(1 + 1) + (3 + 3) = 8

同样,我为N=3绘制了一棵树:

1<=i<=3,        (i=1)-----------------------(i=2)--------------------------------------------(i=3)
                  |                 |---------|------|                  |----------------------|----------------------|
1<=j<=3,        (j=1)             (j=1)            (j=2)            (   j=1   )           (   j=2    )           (   j=3    )
                /   \            /  |  \          /  |  \          / |       |  \        / |       |  \          / |       | \
0<=k<=i,    (K=0)   (K=1)    (K=0)(K=1)(k=2)   (K=0)(K=1)(k=2)    /  |       |   \      /  |       |   \        /  |       |  \
              |       |        |    |    |       |    |    |     (K=0)(K=1)(k=2)(k=3)  (K=0)(K=1)(k=2)(k=3)  (K=0)(K=1)(k=2)(k=3)
r=0          r++     r++      r++  r++  r++     r++  r++  r++     r++  r++   r++  r++   r++  r++  r++  r++   r++  r++   r++   r++

那是(1 + 1) + (3 + 3) + (4 + 4+ 4)= 20

N = 1,   (1 + 1) = 2  
N = 2,   (1 + 1) + (3 + 3) = 8  
N = 3,   (1 + 1) + (3 + 3) + (4 + 4 + 4)= 20  
N = 4,   (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5)  =  40  
N = 5,   (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) = 70  
N = 6,   (1 + 1) + (3 + 3) + (4 + 4 + 4) + (5 + 5 + 5 + 5) + (6 + 6 + 6 + 6 + 6) + (7 + 7 + 7 + 7 + 7 + 7)= 112  

对于N = 6,我们也可以在序列上面写为:

(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + (6*7)    

最后,我可以理解三个循环中N的总和是:

(1*2) + (2*3) + (3*4) + (4*5) + (5*6) + ... + (N * (N+1))

在math.stackexchange.com的帮助下,我可以简化这个等式:
我在这里问:How to simplify summation equation in terms of N?

  

image

因此,当我对您的问题发表评论时,N的结果为( ((N) * (N+1) * (N+2)) / 3 ) 而且,我认为它是正确的。我按如下方式交叉检查:

N = 1,    (1 * 2 * 3)/3  = 2

N = 2,    (2 * 3 * 4)/3 =  8

N = 3,    (3 * 4 * 5)/3 =  20

N = 4,    (4 * 5 * 6)/3 =  40

N = 5,    (5 * 6 * 7)/3 =  70    

答案 1 :(得分:0)

尝试使用这样的代码来解决它...即编码它是什么以及你认为它应该是什么并测试它。

编辑:根据上述评论进行更新。

public class CountLoop{

  public static void main(String[] args){

    for(int i=1;i<=10;i++)
      System.out.println("It's "+run(i)+" and I think "+guess(i));;

  }

  public static int run(int n){

    int r = 0;
    for(int i=1;i<=n;i++)
      for(int j=1; j <= i;j++)
        for(int k=j; k <= i+j; k++)
          r += 1;

    return r;

  }

  public static int guess(int n){

   // taken from the comments
   int r = ((n * (n+1) * (n+2)) /3);

   return r;

  }

}

运行此功能

It's 2 and I think 2
It's 8 and I think 8
It's 20 and I think 20
It's 40 and I think 40
It's 70 and I think 70
It's 112 and I think 112
It's 168 and I think 168
It's 240 and I think 240
It's 330 and I think 330
It's 440 and I think 440

所以我们很开心。

答案 2 :(得分:0)

我得到的是这样的:

n = 1: r = 2
n = 2: r = 8
n = 3: r = 20
n = 4: r = 40
n = 5: r = 70
n = 6: r = 112
n = 7: r = 168
n = 8: r = 240
n = 9: r = 330
n = 10: r = 440

让我们说n = 10,

r = 2 + 6 + 12 + 20 + 30 + 42 + 56 + 72 + 90 + 110 = 440
=> r = 2(1 + 3 + 6 + 10 + 15 + 21 + 28 + 36 + 45 + 55)

直观地说,我认为

n = sum(n-1) + n * (n + 1).

,其中

sum(n-1) = value of r for n-1