题解 | 蛇形矩阵
解题思路:
- 整体并不难,核心是找出其中每一行之间的关系
import java.util.Scanner; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int c = 1; for (int i = 1; i <= n; i++) { System.out.print(c); int r = c; for (int j = i + 1; j <= n; j++) { System.out.print(" "); r = r + j; System.out.print(r); } System.out.println(); c = c + i; } } }