题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
- 按照找规律的思路解决
- 找到规律:行和列都类似斐波那契数列
- 制造规律初始值及递增
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
String s = new BufferedReader(new InputStreamReader(System.in)).readLine();
// 每一行的递增初始值
int row = 1;
// 每一列的递增初始值
int column = 2;
/**
* 左至右递增
* 上至下递增
*/
for (int i = 0; i < Integer.parseInt(s); i++) {
// 行递增并打印
row = row + i;
System.out.print(row + " ");
// 每一行的列递增初始值
int rowTemp = row, columnTemp = column++;
for (int j = i; j < Integer.parseInt(s)-1; j++) {
// 列递增并打印
rowTemp = columnTemp++ + rowTemp;
System.out.print(rowTemp + " ");
}
System.out.println();
}
}
}