测试幻方的 .txt 文件 Java

Testing a .txt file for a Magic Square Java

本来不想问,但是这个作业我想不通,求助的时候TA也想不通

我必须从文本文件中获取输入,将文件中的整数输入数组列表,然后测试它是否是 n x n 幻方。 n 等于数组列表长度的平方根。如果它不是一个完美的正方形,它会立即通过幻方测试。

反正我已经快完成了;我只是似乎不明白我的教授 telling/asking 我们在魔方测试的最后一步要做什么。

最后四个步骤之前的所有测试都完美无缺。我将 post 完成这些步骤后的当前代码。

  1. Let rowSums and colSums be two arrays of length n with entries all zeros. Also, let sumDiagMajor and sumDiagMinor, representing the sum of the entries along the top-left to bottom-right and top-right to bottom-left diagonals, respectively, of the table.

  2. Let index = 0

  3. Repeat until index = n2 (a) Increment rowSums[row] by ArrayList{index} (b) increment colSums[col] by ArrayList{index} (c) If row = col, then increment sumDiagMajor by ArrayList{index}. (d) If row + col = n−1, then increment sumDiagMinor by ArrayList{index} (e) Increment index by 1

  4. If sumDiagMajor is equal to sumDiagMinor and each entry of rowSums and colSums, the the table is a magic square; otherwise, it is not.

   int rowSums[] = new int[_n];
   int colSums[] = new int[_n];
   int sumDiagMajor = 0;
   int sumDiagMinor = 0;

   int row, col;
   row = col = 0;

   for (int index = 0; index < (n*n); index++)
   {          
       rowSums[row] = rowSums[row] + magicSquare.get(index);
       colSums[col] = colSums[col] + magicSquare.get(index);

       if (row == col)
       {       
           sumDiagMajor = sumDiagMajor + magicSquare.get(index);   
       }

       if ((row + col) == (n - 1))
       {
           sumDiagMinor = sumDiagMinor + magicSquare.get(index);   
       }

   }

   System.out.println(sumDiagMajor);
   System.out.println(sumDiagMinor);

我的问题包括,我是否正确递增了数组 rowSums 和 rowCols?他从未真正说明如何处理行或列,所以将它们初始化为零是最好的选择吗?

如果到目前为止我所做的一切都是正确的,sumDiagMajor 怎么会等于 sumDiagMinor,因为行总是等于 cols,所以第二个嵌套的 if 语句永远不会 运行。因此它会排除所有测试都是幻方吗?

抱歉这么长post,但这很混乱。

您永远不会在您的 for 循环中更改 rowcol,这是一个错误。 为了更容易理解,我建议在 rowIndex 和 colIndex 上使用两个嵌套的 for 循环,并使用一个小助手从一维数组中获取值,如下所示:

int getValue(int row, int col){
    return magicSquare.get( row * _n + col );
}

假设一维数组是一个 "flattened" 正方形,其构建方式类似于 (0,0), (0,1), ... (0,_n), (1, 0) , ... (_n,_n)

为了更清楚:迭代正方形而不是使用索引:

for( int row = 0 ; row < _n ; row++){
    for( int col = 0 ; col < _n ; col++){
       // Your stuff here.
    }
}

根据您更新后的要求。一个完整的例子。

public static void main(String[] args) {
    List<Integer> magicSquare = Arrays.asList(2,7,6,9,5,1,4,3,8);

    int n = (int) Math.sqrt(magicSquare.size());
    int rowSums[] = new int[n];
    int colSums[] = new int[n];
    int sumDiagMajor = 0;
    int sumDiagMinor = 0;

    int row = -1;
    int col = -1;

    for (int index = 0; index < n*n; index++) {

        col++;
        if (col % n == 0) {
            row++;
            col = 0;
        }

        rowSums[row] = rowSums[row] + magicSquare.get(index);
        colSums[col] = colSums[col] + magicSquare.get(index);


        if (row == col)
        {
            sumDiagMajor += magicSquare.get(index);
        }

        if ((row + col) == (n - 1))
        {
            sumDiagMinor += magicSquare.get(index);
        }

    }

    boolean isMagicSquare = true;
    for (int i = 0; i < n && isMagicSquare; i++) {
        isMagicSquare = sumDiagMajor == rowSums[i] && sumDiagMajor == colSums[i];
    }
    isMagicSquare = isMagicSquare && sumDiagMajor == sumDiagMinor;

    System.out.println(isMagicSquare); // true
}