public static void main(String[] args) {
System.out.println("请输入n,按回车完成输入:");
Scanner in = new Scanner(System.in);
int n = in.nextInt();
int[][] a = new int[n][n];
int max = n * n;
int row = 0, col = 0;
int direction = 0;
for (int i = 1; i <= max; i++) {
a[row][col] = i;
switch (direction) {
case 0:
if (row+1>=n || a[row + 1][col] != 0) {
direction += 1;
direction %= 4;
row--;
col++;
}
row++;
break;
case 1:
if (col+1 >= n || a[row][col + 1] != 0) {
direction += 1;
direction %= 4;
col--;
row--;
}
col++;
break;
case 2:
if (row-1<0 || a[row - 1][col] != 0) {
direction += 1;
direction %= 4;
row++;
col--;
}
row--;
break;
case 3:
if (col-1<0 || a[row][col - 1] != 0) {
direction += 1;
direction %= 4;
col++;
row++;
}
col--;
break;
default:
System.out.println("你进入了异度空间四边形你却找到了第五条边!");
System.exit(0);
}
}
String string = new String();
String mStr = String.valueOf(max);
for (int j = 0; j < n; j++) {
for (int k = 0; k < n; k++) {
string = String.valueOf(a[j][k]);
while (mStr.length() > string.length()) {
string = "0" + string;
}
System.out.print(" " + string);
}
System.out.println("");
}
}

解决方案 »

  1.   

    这不是NOIP原题吗
    我来写写看
      

  2.   


    import java.util.Scanner;public class Guess { public enum Direction {DOWN,RIGHT,UP,LEFT};
        public static void main(String[] args) {
         Direction direct = Direction.DOWN;
         int size, row, col;
         int count = 0;
         Scanner sc = new Scanner(System.in);
            size = sc.nextInt();
            int rao[][] = new int[size][size];
            for(row = 0; row < size; row++){
             for(col = 0; col < size; col++){
             rao[row][col] = 0;        
             }        
            }
            
            row = 0;
            col = 0;
            
            while(count < size * size){
             rao[row][col] = ++count;
             switch(direct){
             case DOWN:
             if(row+1<size && rao[row+1][col]==0){
             row++;        
             } else {
             col++;
             direct = Direction.RIGHT;
             }
             break;
             case UP:
             if(row-1>=0 && rao[row-1][col]==0){
             row--;
             } else {
             col--;
             direct = Direction.LEFT;
             }
             break;
             case LEFT:
             if(col-1>=0 && rao[row][col-1]==0){
             col--;        
             } else {
             row++;
             direct = Direction.DOWN;
             }
             break;
             case RIGHT:
             if(col+1<size && rao[row][col+1]==0){
             col++;        
             } else {
             row--;
             direct = Direction.UP;
             }
             break;        
             }        
            }
            show(rao);        
        }    public static void show(int a[][]) {
            int row, col;
            for(row=0; row<a.length; row++) {
                for(col=0; col<a[0].length; col++) {
                 System.out.print(a[row][col] + "\t");
                }
                System.out.println();
            }
            return;
        }
    }
      

  3.   


           public static void test(int size) {
    int[][] a = new int[size][size];
    int num = 0;
    for (int i = 0; i < size / 2; i++) {
    for (int j = i; j < size - i; j++) {
    a[j][i] = ++num;
    }
    for (int j = i + 1; j < size - i - 1; j++) {
    a[size - i - 1][j] = ++num;
    }
    for (int j = size - i - 1; j > i - 1; j--) {
    a[j][size - i - 1] = ++num;
    }
    for (int j = size - i - 2; j > i; j--) {
    a[i][j] = ++num;
    }
    }
    if (size % 2 == 1) {
    a[size / 2][size / 2] = ++num;
    }
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    System.out.print(StringUtils.leftPad(a[i][j] + "", 5, " "));
    }
    System.out.println();
    }
    }
      

  4.   

    不谢。public class 花了我一下午 {
        public static void main(String[] args) {
            int size = 6;// size > 1
            int a[][] = new int[size][size];
            int i = 0, j = 0, I = 1, J = 0, s = 1;
            while (a[i][j] == 0) {
                a[i][j] = s++;
                if (i + I >= size || j + J >= size || j + J < 0 || i + I < 0 || a[i + I][j + J] != 0) {
                    int tmp = I;
                    I = ((J == 0) ? 0 : -J);
                    J = tmp;
                }
                i += I;
                j += J;
            }
            for (i = 0; i < size; i++) {
                for (j = 0; j < size; j++)
                    System.out.print(a[i][j] + "\t");
                System.out.println();
            }
        }
    }
      

  5.   

    public class 花了我一下午 {
        public static void main(String[] args) {
            int size = 6;// size > 1
            int a[][] = new int[size][size];
            int i = 0, j = 0, I = 1, J = 0, s = 1;
            while (a[i][j] == 0) {
                a[i][j] = s++;
                if (i + I >= size || j + J >= size || j + J < 0 || i + I < 0 || a[i + I][j + J] != 0) {
                    int tmp = I;
                    I = ((J == 0) ? 0 : -J);
                    J = tmp;
                }
                i += I;
                j += J;
            }
            for (i = 0; i < size; i++) {
                for (j = 0; j < size; j++)
                    System.out.print(a[i][j] + "\t");
                System.out.println();
            }
        }
    }
      

  6.   

    我来一个笨方法  可读性相当强
    public class Circle { public static void main(String[] args) {

    int size = 5;

    int sqr = size * size;

    int[][] arr = new int[size][size];

    String format = String.format("%%%dd", String.valueOf(sqr).length() + 1);

    for(int i = 1, x = 0, y = 0, d = 1; i <= sqr; i++) {
    if(x < size - 1 && arr[x + 1][y] == 0 && d == 1) {
    arr[x][y] = i;
    x++;
    } else {
    d = 2;
    if(y < size - 1 && arr[x][y + 1] == 0 && d == 2) {
    arr[x][y] = i;
    y++;
    } else {
    d = 3;
    if(x > 0 && arr[x - 1][y] == 0 && d == 3) {
    arr[x][y] = i;
    x--;
    } else {
    d = 4;
    if(y > 0 && arr[x][y - 1] == 0 && d == 4) {
    arr[x][y] = i;
    y--;
    } else {
    d = 1;
    if(x < size - 1 && arr[x + 1][y] == 0 && d == 1) {
    arr[x][y] = i;
    x++;
    } else {
    arr[x][y] = i;
    }
    }
    }
    }
    }
    }

    for(int i = 0, j; i < size; i++) {
    for(j = 0; j < size; j++) {
    System.out.format(format, arr[i][j]);
    }
    System.out.println();
    }

    }}
      

  7.   

    能够不能解释下呢?有点不是很明白你的那个判断条件啦
    如果把if判断改为下面这,你应该就明白了吧。两者效果一样,前者简洁,后者直观。实在看不懂,请逐步调试检查数组a的变化,size改成3方便些。            if (i + I >= size || j + J >= size || j + J < 0 || i + I < 0 || a[i + I][j + J] != 0) {
                    if (I == 1) {
                        I = 0;
                        J = 1;
                    } else if (J == 1) {
                        J = 0;
                        I = -1;
                    } else if (I == -1) {
                        I = 0;
                        J = -1;
                    } else if (J == -1) {
                        J = 0;
                        I = 1;
                    }
                }
      

  8.   

    和我想的一样,最终生成的图形是一层层正方形,可以从外到内一一确定正方形每个边的所有数字,从正方形的左上角开始,每个正方形的左上角数字是(4n-3),正方形边上数字个数递减2,如果是n是奇数,中间会多出一个单独的数字n*n。后话:代码细节没处理好