日期:2014-05-20 浏览次数:20828 次
/**
* 程序目的,从1 开始 为数组赋值 赋值方式为蛇形
* 形如
* 10 11 12 1
* 9 16 13 2
* 8 15 14 3
* 7 6 5 4
*
*
*/
import java.util.Scanner;
import java.util.Arrays;
public class LightPro {
public static void main(String args[]) {
// 4 4
int[][] a = new int[4][4];
int x,y;
int num = 0;
num = a[x = 0][y = 3] = 1;
for(int i = 0;i < 4;i++) {
Arrays.fill(a[i],0);
}
while(num < 16) {
while(x + 1 < 4 && a[x + 1][y] == 0) a[++x][y] = ++num;
while(y - 1 >= 0 && a[x][y - 1] == 0) a[x][--y] = ++num;
while(x - 1 >= 0 && 0 == a[x - 1][y]) a[--x][y] = ++num;
while(y + 1 < 4 && 0 == a[x][y + 1]) a[x][++y] = ++num;
}
for(int i = 0;i < 4;i++) {
for(int j = 0;j < 4;j++) {
System.out.print(a[i][j] + " ");
}
System.out.println();
}
}
}
num = a[x = 0][y = 3] = 1;
for(int i = 0;i < 4;i++) {
Arrays.fill(a[i],0);
}