Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

algorithm - Testing a .txt file for a Magic Square Java

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...