//1 16 15 14 13
//2 17 24 23 12
//3 18 25 22 11
//4 19 20 21 10
//5 6 7 8 9
//*************
//1 12 11 10
//2 13 16 9
//3 14 15 8
//4 5 6 7
//*************
//1 8 7
//2 9 6
//3 4 5
想要个算法
//2 17 24 23 12
//3 18 25 22 11
//4 19 20 21 10
//5 6 7 8 9
//*************
//1 12 11 10
//2 13 16 9
//3 14 15 8
//4 5 6 7
//*************
//1 8 7
//2 9 6
//3 4 5
想要个算法
public class Test {
public static void main(String[] args) {
int n = 7;
int[][] m = new int[n][n];
int[] d = new int[]{ 1, 0 , 0, 1, -1, 0, 0, -1};
int[] a = new int [n * 2];
a[0] = n - 1;
a[n * 2 -1 ] = 1;
int s = n;
for (int i = 1; i < n * 2 -1 ; i += 2) {
a[i] = a[i + 1] = --s;
}
int c = 0;
int w = 1;
int x = 0;
int y = 0;
for (int i = 0; i < a.length; ++i) {
c %= 4; for (int j = 0; j < a[i]; j++) {
m[x][y] = w++;
x += d[c * 2];
y += d[c * 2 + 1];
}
c++;
} for (int k = 0; k < n; k++) {
for (int j = 0; j < n; j++) {
System.out.print(String.format("%4d", m[k][j]));
}
System.out.println();
}
}
}
http://topic.csdn.net/u/20080114/22/48153ab5-a7ea-4246-972b-5e4c9f868430.html