题解 | #蛇形矩阵#
蛇形矩阵
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; } } } }