Testing a .txt file for a Magic Square Java

后端 未结 2 784
一向
一向 2020-12-21 11:46

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

2条回答
  •  借酒劲吻你
    2020-12-21 12:01

    As per your updated requirements. A full example.

    public static void main(String[] args) {
        List 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
    }
    

提交回复
热议问题