有人可以解释一下下面的代码是如何工作的。


  问题:连续有64扇门,这些门最初都是关闭的。您在门口经过了64张。第一次访问时,您访问每个门并切换门(如果门是关闭的,则将其打开;如果门是打开的,则将其关闭)。第二次您仅访问第二个门(门#2,#4,#6,...)。第三次,每隔第3个门(第3,#6,#9,...),等等,直到您只访问第64个门。


我发现以下代码可以正常工作,但是我想知道到底发生了什么。我不明白循环。

public class doors {
public static void main(String args[]) {
    // assume true=open, false=closed. Doors will all default to closed.
    boolean[] doors = new boolean[64];
    for (int i=0; i<64; i++) {
        for (int j=i; j<64; j=j+i+1) {
            doors[j] = !doors[j];
        }
    }
    // at the end, print out all the doors that are closed
    System.out.println("These doors are opened:");
    for (int i=0;i<64;i++){
        if (doors[i]) {
            // printing out i+1 to negate the zero-indexing
            System.out.println(i+1);
        }
    }
}
}

最佳答案

假设您仍然知道for loops的工作原理,让我们看下面的示例:

首先,假设所有门均已关闭,关闭表示false f,打开表示true t。为了简化理解,我们还假设门总数= 4

index       :     0       1       2       3
              +-----+ +-----+ +-----+ +-----+
begin       : |  f  | |  f  | |  f  | |  f  | // say all doors closed
              +-----+ +-----+ +-----+ +-----+
door number :    1       2       3       4


现在

 for (int i=0; i<4; i++) { // our example has 4 doors instead of 64
        // first iteration of outer loop, i = 0
        for (int j=i; j<4; j=j+i+1) {
            // as i = 0, j = 0 too
            // and j = j+i+1 = j + 0 + 1 = j +1
            // so j will increment by 1
            // hence, j < 4 means the loop will rotate 4 (j = 0 to 3) times
            doors[j] = !doors[j]; // this line do the trick, See bellow for details.
        }
    }


doors[j] = !doors[j];切换当前状态。怎么样?假设doors[j]包含false意味着门已关闭,然后!doors[j]将值false更改为true意味着门已关闭!塔达,这就是我们想要的!

   index(value of j) :    0       1       2       3
                       +-----+ +-----+ +-----+ +-----+
after 1st iteration  : |  t  | |  t  | |  t  | |  t  |
                       +-----+ +-----+ +-----+ +-----+
         door number :    1       2       3       4


全部四个门打开!

现在,对于外循环的第二次迭代,

 for (int i=0; i<4; i++) { // our example has 4 doors instead of 64
        // 2nd iteration of outer loop, i = 1
        for (int j=i; j<4; j=j+i+1) {
            // as i = 1, j = 1 too
            // and j = j+i+1 = j + 1 + 1 = j + 2
            // so j will increment by 2
            // hence, j < 4 means the loop will rotate 2 (j = 1 and j = 3) times
            doors[j] = !doors[j];
        }
    }


所以,

   index(value of j) :    0       1       2       3
                       +-----+ +-----+ +-----+ +-----+
after 2nd iteration  : |  t  | |  f  | |  t  | |  f  |
                       +-----+ +-----+ +-----+ +-----+
         door number :    1       2       3       4


只有2个和4个数字门关闭!是的,我们步入正轨!

现在您清楚地了解到,在外循环的第三次迭代中,j将以值2开头,并以3递增,这意味着仅门3将被切换!

   index(value of j) :    0       1       2       3
                       +-----+ +-----+ +-----+ +-----+
after 3rd iteration  : |  t  | |  f  | |  f  | |  f  |
                       +-----+ +-----+ +-----+ +-----+
         door number :    1       2       3       4


希望这将帮助您了解此代码如何通过64门解决问题!

最终的(第4次)迭代将为您打开所有大门,如下所示:

   index(value of j)   :    0       1       2       3
                         +-----+ +-----+ +-----+ +-----+
after final iteration  : |  t  | |  f  | |  f  | |  t  |
                         +-----+ +-----+ +-----+ +-----+
         door number   :    1       2       3       4

08-18 11:48