日期:2014-05-20 浏览次数:20648 次
public static void main(String[] args) { int[][] data = new int[10][10]; sf(data, 0, 1); for (int i = 0; i < data.length; i++) { for (int j = 0; j < data.length; j++) { if (data[i][j] == 0) { break; } System.out.printf("%03d,",data[i][j]); } System.out.println(); } } //递归吧,好理解 //可以看成是多个同心三角形 //每个边长度为f-1 //每一层第一个顶点为f,f static void sf(int[][] m, int f, int s) { //边长度-1,周长刚刚好为3*floo int floo = m.length - f * 3 - 1; //横 for (int i = 0; i <= floo; i++) { m[f][f + i] = i + s; } //斜 for (int i = 0; i < floo; i++) { m[f + i][f + floo - i] = floo + i + s; } //纵 for (int i = 0; i < floo; i++) { m[f + floo - i][f] = floo + floo + i + s; } if (m.length - f * 3 > 3) { sf(m, f + 1, s + floo * 3); } }