题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String str = br.readLine(); int num = Integer.parseInt(str); // y记录每行的第一个数 int y = 1; for(int i = 1; i <= num; i++){ // 从每行的第一个数开始输出 int x = y; for(int j = 1; j <= num - i + 1; j++){ System.out.print(x + " "); x += (i + j); } System.out.println(); y += i; } } }