题解 | #蛇形矩阵#

蛇形矩阵

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

import java.util.*;
import java.io.*;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        try {
            Integer num = Integer.valueOf(br.readLine());
            int[][] a = new int[num][];//之前行与列大小都写成了num,但每一行并不是都是一样长的,因为输出可以看到并没有被0填满;
            int t=1;
            for (Integer i = 0; i < num; i++) {
                a[i]=new int[num-i];//所以这里对每一行数组的长度进行动态赋值;
                for (Integer j = 0; j < i+1; j++) {
                    a[i-j][j]=t++;
                }
            }
            for (int[] b : a) {
                for (int i : b) {
                    System.out.print(i+" ");
                }
                System.out.println();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

全部评论

相关推荐

09-15 15:53
Java
Elastic90:我看到的是东软的人在耐心回应,而那位实习生跟在发疯似的
投递东软集团等公司10个岗位
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务