如题 :
1  2  3  4  5  
16 17 18 19 6      JAva 中怎么用2维数组把这些
15 24 25 20 7      数字按顺序打印出来呀。
14 23 22 21 8       这其中有什么规律哦。
13 12 11 10 9       请帮忙分析 写出来 ..谢谢哦. 新人请大家多帮忙.

解决方案 »

  1.   

    你把从1-25的数字数一遍就看出来什么叫螺旋了,然后找规律添数组,呵呵import javax.swing.JOptionPane; public class Circle { int[][] numArray; 
    int record = 1;//记录每层的起始数字 
    int stepF = 0;//每层的起始下标 
    int stepE = 0;//每层的终止下标 
    int reduce = 0;//用于记录减小量 public static void main(String args[]) { new Circle().process(); } public void setNum(int num) {//递归的为二维数组赋值 
    if (num > 0) {//只要层数是正数继续赋值 for (int i = stepF; i < stepE; i++) {//为每层的第一列和最后一列赋值 
    numArray[i][stepF] = record; 
    numArray[i][stepE - 1] = record + 3 * (num - 1) - 2 * reduce; 
    reduce++; 
    record++; 

    reduce = 1; 
    for (int i = stepF + 1; i < stepE - 1; i++) {//为每层的第一行和最后一行赋值 
    numArray[stepE - 1][i] = record; 
    numArray[stepF][i] = record + 3 * (num - 1) - 2 * reduce; 
    reduce++; 
    record++; 

    reduce = 0; 
    stepF++; 
    record = record + 2 * (num - 1);//得到下一层的起始量 
    stepE--; 
    setNum(num - 2);//每递归一次每层边上的数字量减小2 
    } else//当边数含有的数字量非正时退出递归 
    return; 
    } public void process() {//将数组中的数字按行列的形式输出 
    String s = JOptionPane.showInputDialog("输入矩阵"); 
    int num = Integer.parseInt(s); 
    numArray = new int[num][num]; 
    String space = ""; 
    stepE = num; 
    setNum(num); 
    for (int i = 0; i < num; i++) { 
    for (int j = 0; j < num; j++) { 
    if (numArray[i][j] < 10 && j > 0) {//保证输出是个正方形,不会因为是一位数或者2位数影响效果 
    space = " "; 
    } else 
    space = " "; 
    System.out.print(numArray[i][j] + space); 

    System.out.println();//输入完一行后要换行 

    } } 
      

  2.   

    一牛人的C实现copy过来的那里还讲了一般的情况,可以去看看
    http://www.neu.edu.cn/cxsj/ShowBlog.aspx?ID=217&BlogID=10public class Test {
    static final int N = 5;
    public static void main(String[] args) {
    int n, i, j, m = 1;
    int a[][] = new int[N][N];// 定义螺旋数组
    // 螺旋数组的实现
    for (n = 0; n <= N / 2; n++) {
    for (j = n; j < N - n; j++)
    a[n][j] = m++;
    for (i = n + 1; i < N - n; i++)
    a[i][N - n - 1] = m++;
    for (j = N - n - 2; j >= n; j--)
    a[N - n - 1][j] = m++;
    for (i = N - n - 2; i > n; i--)
    a[i][n] = m++;
    }
    // 输出螺旋数组
    for (i = 0; i < N; i++) {
    for (j = 0; j < N; j++)
    System.out.printf("%4d", a[i][j]);
    System.out.printf("\n");
    }
    }
    }