题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
package OnlineTest.easy; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.Scanner; public class HJ35 { public static void main(String[] args) throws IOException { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); //获取行数 int line = Integer.parseInt(bf.readLine()); // int first = 1; int cal=first; int lineNext; for (int i = 0; i < line; i++) {//输出第i行 cal = cal + i;//输出第i行的第一个数 lineNext=cal; System.out.print(cal+" "); for (int j = 1; j < line - i; j++) {//输出第i行除了第一个数 lineNext=lineNext+(i+1)+j; System.out.print(lineNext+ " "); } System.out.println();//断行; } } }