题解 | #蛇形矩阵#

蛇形矩阵

https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e

import java.util.Scanner;

// 00 | 10 01 | 20 11 02 | 30 21 12 03 | ...
public class Main {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        int num = in.nextInt();
        int[][] array = new int[num][num];

        int start = 1;
        for(int i=0; i<num; i++){
            for(int j=i, k=0; j>=0; j--,k++){
                array[j][k] = start++;
            }
        }

        for(int i=0; i<num; i++){
            for(int j=0; j<num-i; j++){
                System.out.print(array[i][j]+" ");
            }
            System.out.println();
        }
    }
}

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务