【问题标题】:Why -doesn't- this while loop endlessly loop?为什么-不-这个while循环无限循环?
【发布时间】:2018-03-17 19:22:24
【问题描述】:

*编辑附加到帖子底部。

我试图在 C 中生成一些代码,以生成具有 10 行和 2 列的二维数组。我创建了一个检查,以查看在生成一行时,如果它与另一个现有行相同,它将不断更改它的值,直到它成为一个新的唯一行。

我目前只将最大变量设置为 1,以验证 while 循环永远不会终止,因为它可能找不到 0 和 1 的 10 个唯一组合。这段代码确实确实找到了重复项并进行了相应的更改,但似乎最终跳过了检查并终止,而不是像它应该的那样无休止地循环。 printf 函数用于探测我的代码的行为。

为什么我的 while 循环终止了?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <time.h>

int main (int argc, char *argv[]) {
    int max_x = 1, max_y = 1;
    int num_pt = 10;
    int array[num_pt][2];
    srand(time(0));

    //Generate a new file
    if (argc == 1) {
        printf("Generating instances\n");
        for (int i = 0 ; i < num_pt ; i++) {
            array[i][0] = rand()%(max_y+1);
            array[i][1] = rand()%(max_x+1);
            for (int j = i-1 ; j >= 0 ; j-- ) {
                while ((array[i][0] == array[j][0]) && (array[i][1] == array[j][1])) {
                    printf("\narray[%d][0]/[%d][1] and array[%d][0]/[%d][1]: Duplicate exists", i, i, j, j);
                    array[i][0] = rand()%(max_y+1);
                    array[i][1] = rand()%(max_x+1);
                }
            }
        }
        for (int j = 0 ; j < num_pt ; j++) {
            printf("\nRow[%d] = [%d][%d]", j, array[j][0], array[j][1]);
        }
    return 1;
    }
}

样本输出:

Generating instances

array[2][0]/[2][1] and array[0][0]/[0][1]: Duplicate exists
array[2][0]/[2][1] and array[0][0]/[0][1]: Duplicate exists
array[3][0]/[3][1] and array[1][0]/[1][1]: Duplicate exists
array[4][0]/[4][1] and array[1][0]/[1][1]: Duplicate exists
array[5][0]/[5][1] and array[1][0]/[1][1]: Duplicate exists
array[6][0]/[6][1] and array[4][0]/[4][1]: Duplicate exists
array[6][0]/[6][1] and array[2][0]/[2][1]: Duplicate exists
array[6][0]/[6][1] and array[0][0]/[0][1]: Duplicate exists
array[7][0]/[7][1] and array[6][0]/[6][1]: Duplicate exists
array[7][0]/[7][1] and array[0][0]/[0][1]: Duplicate exists
array[8][0]/[8][1] and array[4][0]/[4][1]: Duplicate exists
array[8][0]/[8][1] and array[2][0]/[2][1]: Duplicate exists
array[8][0]/[8][1] and array[0][0]/[0][1]: Duplicate exists
array[9][0]/[9][1] and array[8][0]/[8][1]: Duplicate exists
array[9][0]/[9][1] and array[6][0]/[6][1]: Duplicate exists
array[9][0]/[9][1] and array[6][0]/[6][1]: Duplicate exists
array[9][0]/[9][1] and array[0][0]/[0][1]: Duplicate exists
array[9][0]/[9][1] and array[0][0]/[0][1]: Duplicate exists
Row[0] = [1][1]
Row[1] = [1][0]
Row[2] = [0][0]
Row[3] = [0][1]
Row[4] = [0][1]
Row[5] = [0][0]
Row[6] = [1][0]
Row[7] = [0][0]
Row[8] = [0][1]
Row[9] = [0][1]

------------------
(program exited with code: 1)
Press return to continue

感谢您的帮助。当我昨晚躺在床上想着这件事时,我终于明白了。我的解决方案是添加一个计数器,如果它发现任何多余的行,它将重新运行整个检查。

int main (int argc, char *argv[]) {
    int max_x = 2, max_y = 2;
    int num_pt = 9;
    int array[num_pt][2];
    srand(time(0));
    int rerun = 1;

    //Generate a new file
    if (argc == 1) {
        printf("Generating instances\n");
        for (int i = 0 ; i < num_pt ; i++) {
            array[i][0] = rand()%(max_y+1);
            array[i][1] = rand()%(max_x+1);
            rerun++;
            while (rerun != 0) {
                rerun = 0;
                for (int j = i-1 ; j >= 0 ; j-- ) {
                    if (array[i][0] == array[j][0] && array[i][1] == array[j][1]) {
                        printf("\narray[%d][0]/[%d][1] and array[%d][0]/[%d][1]: Duplicate exists", i, i, j, j);
                        array[i][0] = rand()%(max_y+1);
                        array[i][1] = rand()%(max_x+1);
                        rerun++;
                    }
                }
            }
        }
        for (int j = 0 ; j < num_pt ; j++) {
            printf("\nRow[%d] = [%d][%d]", j, array[j][0], array[j][1]);
        }
    return 1;
    }
}

这种方法可行,但如果有人有更有效的方法,我会全力以赴。

【问题讨论】:

  • 为什么不在调试器中逐行执行代码?这样您就可以轻松了解实际情况。

标签: c


【解决方案1】:

循环顺序错误。

您的内部 while 循环只会一遍又一遍地检查同一对,直到找到该特定对的有效组合。

当它发生时,它可能已经破坏了与另一个元素的兼容性。由于您的外部和中间循环将永远不会重新访问已经工作过一次的对,这完全不会被注意到。

【讨论】:

    【解决方案2】:

    很简单,当您尝试查看第 i 行和第 j 行是否相似时,您可以更改第 i 行中的值。

    然后 j = j - 1。现在您将这个新 j 与第 i 行进行比较。如果这些恰好相似,那么您再次更改第 i 行,但这次您不会查看第 j 行的先前值。

    创建新行时,您必须记住每行的所有使用值。但是当前算法一次只查看 2 行。

    这就是我所说的情况,

          1 0
    j  -- 0 1
    i  -- 0 1
    
    row i == row j, change row i
    
          1 0
    j  -- 0 1
    i  -- 1 0
    
    j --;
    
    j  -- 1 0
          0 1
    i  -- 1 0
    
    row i == row j, change row i
    
    j --  1 0
          0 1
    i  -- 0 1
    

    这里循环正确中断,因为第 i 行!= 第 j 行,但第 i - 1 行仍然与第 i 行相似。

    【讨论】:

      猜你喜欢
      • 2016-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2016-07-14
      • 2012-05-02
      • 1970-01-01
      相关资源
      最近更新 更多