题解 | #蛇形矩阵#

蛇形矩阵

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

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int N = sc.nextInt();
        int[][] a = new int[N + 1][N + 1];
        int p = 0;
        for (int i = 1; i <= N; i++) {
            int j = i, k = 1;
            while (j >= 1 && k <= i) {
                a[j][k] = ++p;
                j--;
                k++;
            }
        }
        for (int i = 1; i <= N; i++) {
            for (int j = 1; j <= N - i + 1; j++) {
                System.out.print(a[i][j] + " ");
            }
            System.out.println();
        }
    }
}

全部评论

相关推荐

jack_miller:杜:你不用我那你就用我的美赞臣
点赞 评论 收藏
分享
点赞 收藏 评论
分享
牛客网
牛客企业服务