public class Count {
public static void print(int count) {
int is[][] = new int[count][count];
int i = 0;
int c = count * count;
// 横向坐标累加器
int j = 0;
// 纵向坐标累加器
int k = 0;
// 横纵向控制,1为横向,-1为纵向
int m = 1;
// 坐标累加器,1为递增,-1为递减
int n = 1;
while (i < c) {
is[j][k] = ++i;
if (m > 0) {
k += n;
// 触边转向
if (k < 0 || k >= count || is[j][k] != 0) {
m *= -1;
k -= n;
j += n;
}
} else {
j += n;
// 触边转向
if (j < 0 || j >= count || is[j][k] != 0) {
m *= -1;
j -= n;
n *= -1;
k += n;
}
}
}
for (int p = 0; p < count; ++p) {
for (int q = 0; q < count; ++q)
System.out.print(is[p][q] + "/t");
System.out.println();
}
}
public static void main(String[] args) {
Count hCount = new Count();
hCount.print(7);
}
}