题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
while (scan.hasNext()) {
int n = scan.nextInt();
int[][] arr = new int[n][n];
arr[0][0] = 1;
for (int i = 0; i < n; i++) {
StringBuffer sb = new StringBuffer();
if (i > 0) {
arr[i][0] = arr[i - 1][0] + i;
}
sb.append(arr[i][0]).append(" ");
for (int j = 1; j < n - i; j++) {
arr[i][j] = arr[i][j - 1] + i + j + 1;
sb.append(arr[i][j]).append(" ");
}
System.out.println(sb);
}
}
}
}