[PTA] Judging Bisymmetric Square Matrix

Please add image description
Personal business card:

blogger: Alcoholics ᝰ.
Personal profile: Indulge in wine, and strive for a future with a drink.
column: PTA exercises and analysis
introduce: Recorded the blogger's practice questions in pta

Please add image description

foreword

1 Introduction

"PTA programming experimental auxiliary teaching platform" is an auxiliary teaching platform for programming courses led by Zhejiang University, managed and operated by Hangzhou Baiteng Education Technology Co., Ltd., and jointly constructed by teachers in colleges and universities across the country. At present, 538 colleges and universities across the country have participated, and 3,152 teachers have jointly constructed 66,095 high-quality topics, and the number of registered students has reached 1.36 million; there are 14 fixed topic sets (covering C language, JAVA language, Python language, data structure, database system) and China University Computer Competition Question Bank) and 55 professional course topic sets (covering computer, electronics, literature, foreign language and Huawei certification). There are 10 types of questions including true and false questions, fill-in-the-blank questions, multiple-choice questions, multiple-choice questions, program fill-in-the-blank questions, function questions, programming questions, subjective questions, multi-file programming questions and SQL programming questions.

2. Advantages

  • Support 200,000 people online at the same time
  • Support multiple question types
  • Support a variety of judgment modes
  • Provide comprehensive guarantee for teaching quality
  • Instant question answering system

1. The topic

For a square matrix of order n, please judge whether the square matrix is ​​double symmetric, that is, it is symmetrical both left and right and up and down. If so, output "yes", otherwise output "no". For example, in the example, the left and right symmetry is bounded by the second column, and the upper and lower symmetry is bounded by the second row, so the output "yes".

Input format:
first enter a positive integer T, which represents the number of test data groups, and then T groups of test data. The first row of each set of data is input with the order n of the square matrix (2≤n≤50), and the next input is n rows, each row contains n integers, representing the elements in the square matrix.

Output format:
For each set of test data, if the square matrix is ​​double symmetric, output "yes", otherwise output "no". Note that the quotes do not have to be output.

Input sample:

2
3
1 2 1
3 5 3
1 4 1
3
1 2 1
3 5 3
1 2 1

Sample output:

no
yes

Code Length Limit 16 KB
Time Limit 400 ms
Memory Limit 64 MB

2. Code

import java.util.Scanner;

public class Main {
    
    
    public static void main(String[] args) {
    
    
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for (int k = 0; k < T; k++) {
    
    
            int n = sc.nextInt();
            if (n >= 2 && n <= 50){
    
    
                int[][] num = new int[n][n];
                for (int i = 0; i < n; i++) {
    
    
                    for (int j = 0; j < n; j++) {
    
    
                        num[i][j] = sc.nextInt();
                    }
                }
                boolean flag = true;
                //竖
                for (int i = 0; i < n; i++) {
    
    
                    for (int j = 0; j < n/2; j++) {
    
    
                        if (!(num[i][j] == num[i][n-j-1])){
    
    
                            flag = false;
                        }
                    }
                }
                //横
                for (int i = 0; i < n; i++) {
    
    
                    for (int j = 0; j < n/2; j++) {
    
    
                        if (!(num[j][i] == num[n-j-1][i])){
    
    
                            flag = false;
                        }
                    }
                }
                if (flag){
    
    
                    System.out.println("yes");
                }else {
    
    
                    System.out.println("no");
                }
            }
        }
    }
}

3. Notes

  1. A symmetric matrix refers to a matrix with the main diagonal as the symmetry axis, and each element corresponds to the same matrix. In linear algebra, a symmetric matrix is ​​a square matrix whose transpose is equal to itself.
  2. The product of two symmetric matrices is symmetric if and only if the multiplication of the two is commutative. Two real symmetric matrix multiplications are commutative if and only if their eigenspaces are the same.
  3. Any square matrix X whose elements belong to a field with eigenvalues ​​other than 2 (such as real numbers) can be written as the sum of a symmetric matrix and an obliquely symmetric matrix in exactly one way
  4. A matrix is ​​both symmetric and obliquely symmetric if and only if all elements are zero.

おすすめ

転載: blog.csdn.net/m0_65144570/article/details/127098762