【PTA】Snake 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

A serpentine matrix is ​​an upper triangular matrix consisting of natural numbers starting with 1 (see output example). Requires an integer n, constructs and outputs a serpentine matrix.

Input format:
first enter a positive integer T, which represents the number of test data groups, and then T groups of test data. Enter a positive integer N (N not greater than 100) for each set of tests.

Output format:
For each set of tests, output a serpentine matrix with a total of N rows. Leave a space between every two numbers on each line.

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

1
5

Sample output:

1 3 6 10 15
2 5 9 14
4 8 13
7 12
11

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();
            int[][] num = new int[N][N];
            int sum = 0;
            int sum1 = 0;
            for (int i = 0; i < N; i++) {
    
    
                for (int j = 0; j < N-i; j++) {
    
    
                    if (j == 0){
    
    
                        if (i == 0){
    
    
                            sum = 1;
                            num[i][0] = i+sum;
                            sum = sum + i;
                        }else {
    
    
                            num[i][0] = i+sum;
                            sum = sum + i;
                        }
                    }else {
    
    
                        sum1 = j + i + 1;
                        num[i][j] = num[i][j-1] + sum1;
                    }
                }
            }
            for (int i = 0; i < N; i++) {
    
    
                for (int j = 0; j < N - i; j++) {
    
    
                    System.out.print(num[i][j]);
                    if (j != N - i - 1){
    
    
                        System.out.print(" ");
                    }
                }
                System.out.println();
            }
        }
    }
}

Guess you like

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