Huawei OD machine test - snake matrix (C++ & Java & JS & Python)

describe

The serpentine matrix is ​​a triangular matrix formed by the natural numbers starting from 1 arranged in sequence.

For example, when 5 is entered, the triangle that should be output is:

1 3 6 10 15

2 5 9 14

4 8 13

7 12

11

Enter a description:

Enter a positive integer N (N is not greater than 100)

Output description:

Output a snake matrix with N rows.

Example 1

enter:

4

output:

1 3 6 10
2 5 9
4 8
7

Java:

import java.util.Scanner;

public class Main{
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        while(in.hasNextInt()){
            int n = in.nextInt();    //读入正整数n
            
            int[][] result = new int[n][];    //建立数组(n行)
            int t = 1;    //记录依次赋予的数组值
            for(int i=0; i < n; i++){
                result[i] = new int[n-i];    //数组第i行有n-i个元素
                for(int j=0; j < i+1; j++){    //对第i个对角线赋值
      

Guess you like

Origin blog.csdn.net/m0_68036862/article/details/132716696