【PTA】Verify the palindrome and judge the 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. Verify the palindrome

1. Topics

Write a program that verifies whether a string is a palindrome: reading it before and reading it backward is the same. For example, mom, dad are palindrome strings. The program receives the string input by the user, judges whether it is a palindrome, and then outputs the judgment result.

Input format:
Input gives a string in one line.

Output format:
if the input string is a palindrome, output yes; otherwise, output no.

Sample Input:
Here is a set of inputs. E.g:

mom

Sample output:
The corresponding output is given here. E.g:

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);
        String str = sc.next();
        String sum = String.valueOf(str.charAt(0));
        for (int i = 1; i < str.length(); i++) {
    
    
            sum = sum + str.charAt(i);
        }
        if (str.equals(sum)){
    
    
            System.out.println("yes");
        }else {
    
    
            System.out.println("no");
        }
    }
}

Second, determine the double symmetric square matrix

1. Topics

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

Guess you like

Origin blog.csdn.net/m0_65144570/article/details/127115289