题解 | #蛇形矩阵#

蛇形矩阵

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

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int num = scanner.nextInt();
        shapeSnakeTriangle(num);
    }

    public static void shapeSnakeTriangle(int num) {
        List<Integer> next = new ArrayList<>();
        for (int j = num; j > 0; j--) {
            // 先得到 第一行的数据
            if (j == num) {
                int x = 0;
                for (int i = 1; i <= j; i++) {
                    x = x + i;
                    System.out.print(x);
                    System.out.print(" ");
                    if (i > 1) {
                        next.add(x - 1);
                    }
                }
                System.out.println();
            } else {
                List<Integer> temp = new ArrayList<>();
                for (int i = 0; i < next.size(); i++) {
                    System.out.print(next.get(i) + " ");
                    if (i > 0) {
                        temp.add(next.get(i) - 1);
                    }
                }
                System.out.println();
                next = temp;
            }


        }

    }
}

全部评论

相关推荐

Jcwemz:中软证书写单行,考了什么学了什么相关技术栈的内容就说自己会什么, 没实习就包装实习简历,将项目经历写成实习做的,项目时间拉长,项目成果具体化,测试的项目成果无非就是写了多少用例查出了多少bug,重要的不是实习了多久,而是你会多少东西,你能表达的就都是你的。 cet4,随便找个地方标上就好了,不用写单行。 粗略建议,我也不在行,觉得对的可以采纳
实习,投递多份简历没人回...
点赞 评论 收藏
分享
10-31 13:04
南华大学 Java
嵌入式的小白:很多面试,面试前不会去打扰cto的,但一般cto不会在这些小事上刷人,只能说这个cto比较操心,啥重要不重要,紧急不紧急的,估计都会过问,平淡看待吧
点赞 评论 收藏
分享
评论
1
收藏
分享

创作者周榜

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