为 Magic Square Java 测试 .txt 文件 [英] Testing a .txt file for a Magic Square Java

查看:21
本文介绍了为 Magic Square Java 测试 .txt 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我本来不想问,但我想不通这个作业,我求助的时候助教也搞不定.

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

反正我已经快完成了;我只是似乎不明白我的教授在幻方测试的最后一步告诉/要求我们做什么.

在这最后四个步骤之前的所有测试都完美无缺.我会在这些步骤之后发布我当前的代码.

<块引用>

  1. 令 rowSums 和 colSums 是两个长度为 n 且条目全为零的数组.另外,让 sumDiagMajor 和 sumDiagMinor,代表总和沿着左上角到右下角和右上角的条目分别为表格的左下角对角线.

  2. 让索引 = 0

  3. 重复直到 index = n2 (a) 通过 ArrayList{index} 增加 rowSums[row] (b) 通过 ArrayList{index} 增加 colSums[col] (c)如果 row = col,则通过 ArrayList{index} 增加 sumDiagMajor.(d) 如果row + col = n−1,然后通过 ArrayList{index} (e) 增加 sumDiagMinor将索引增加 1

  4. 如果 sumDiagMajor 等于 sumDiagMinor 且 rowSums 和 colSums 的每一项,则该表是一个幻方;否则,它不是.

 int rowSums[] = new int[_n];int colSums[] = new int[_n];int sumDiagMajor = 0;int sumDiagMinor = 0;整数行,列;行 = 列 = 0;for (int index = 0; index <(n*n); index++){rowSums[row] = rowSums[row] + magicSquare.get(index);colSums[col] = colSums[col] + magicSquare.get(index);如果(行 == 列){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 语句将永远不会运行.因此它会排除所有测试都是幻方吗?

抱歉,这篇文章太长了,但这很令人困惑.

解决方案

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

public static void main(String[] args) {列表<整数>魔方 = 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;整数行 = -1;int col = -1;for (int index = 0; index < n*n; index++) {颜色++;如果(列%n == 0){行++;列 = 0;}rowSums[row] = rowSums[row] + magicSquare.get(index);colSums[col] = colSums[col] + magicSquare.get(index);如果(行 == 列){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);//真的}

I didn't want to have to ask, but I can not figure out this assignment, and neither could the TA when I asked for help.

I have to take input from a text file, feed the integers in the file into an array list, and test to see if it is a n x n magic square. n is equal to the square root of the array list's length. If it isn't a perfect square it immediately fails the magic square test.

Anyway I have it almost completed; I just don't seem to understand what my professor is telling/asking us to do in the final step of the magic square test.

All the testing before these final four steps works flawlessly. I'll post my current code after the steps.

  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);

My questions include, am I properly incrementing the arrays rowSums, and rowCols? He never actually states what to do with rows or cols, so is initializing them to zero the best option?

If I did everything correct so far, how can sumDiagMajor ever equal sumDiagMinor because rows will always equal cols, so the second nested if statement will never run. Therefore it will rule out everything test as being a magic square?

Sorry for the long post, but this is very confusing.

解决方案

As per your updated requirements. A full example.

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
}

这篇关于为 Magic Square Java 测试 .txt 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆