题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
找规律,每一行都是 base + step。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
int input = sc.nextInt();
StringBuilder builder = new StringBuilder();
int base = 1;
for (int i = 0; i < input; i++) {
base += i;
builder.setLength(0);
builder.append(base);
int prev = base;
for (int step = i + 2; step <= input; step++) {
int current = prev + step;
prev = current;
builder.append(" " + current);
}
System.out.println(builder);
}
}
}
}