A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

© 陈晓东 黑马帝   /  2011-10-13 17:26  /  6240 人查看  /  8 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

用户输入一个数 就能打印出这个数的方阵
例如 用户输入4 打印结果就是
1      2      3      4
12    13    14    5
11    16    15    6
10     9       8    7
用递归和二维数组实现。

8 个回复

倒序浏览
本帖最后由 梁锡伟 于 2011-10-13 18:00 编辑

。。。两个for循环??不知道行不行。
for(int i=1; i<=n; i++) {
   for(int j=1; j<=n; j++) {
        syso(i);
        if(i%4==0)
           printline();
            
   }
}
回复 使用道具 举报
....sorry,我看走眼了。。。螺旋
回复 使用道具 举报
本帖最后由 梁锡伟 于 2011-10-13 18:13 编辑

思路是创建一个int[n][n]的两维数组。
奇数的时候从头赋值,偶数的时候从尾部赋值。

试下....没IDE,就不验证了

int [][] arr = int [n][n] ;
for(int i=1; i<=n; i++) {
     if(n为奇数){
        for(int j=1; j<=n; i++) {
             syso(i);
         }
     }else  
         for(int j=n; j>0;j--)
              syso(i);
}

。。。。忘记换行了。。。把上面那个换行的思路copy进去就算完成了吧

评分

参与人数 1技术分 +2 收起 理由
admin + 2

查看全部评分

回复 使用道具 举报
本帖最后由 陈晓东 于 2011-10-15 16:46 编辑
  1. public class FillArray {

  2.         private int[][] array ;
  3.         private int max ;
  4.         private int currentNum;
  5.        
  6.         public FillArray(int len){
  7.                 this.array = new int[len][len];
  8.                 this.max = len*len;
  9.         }
  10.        
  11.         public void fill(int i,int j){
  12.                 // 若数组还没有填充完毕。
  13.                 if(this.currentNum<this.max){
  14.                         // 从左到右填充。
  15.                         for(;j<this.array.length && this.array[i][j]==0;j++){
  16.                                 this.array[i][j]= ++this.currentNum;
  17.                         }
  18.                         // 调整位置。
  19.                         i++ ;  j--;
  20.                         // 从上到下填充。
  21.                         for(;i<this.array.length && this.array[i][j]==0;i++){
  22.                                 this.array[i][j] = ++this.currentNum;
  23.                         }
  24.                         // 调整位置。
  25.                         i--;  j--;
  26.                         // 从右到左填充。
  27.                         for(;j>=0 && this.array[i][j]==0;j--){
  28.                                 this.array[i][j] = ++this.currentNum;
  29.                         }
  30.                         // 调整位置。
  31.                         j++;  i--;
  32.                         // 从下到上填充。
  33.                         for(;i>=0&&this.array[i][j] ==0;i--){
  34.                                 this.array[i][j] = ++this.currentNum;
  35.                         }
  36.                         // 调整位置。
  37.                         i++;  j++;
  38.                        
  39.                         // 递归填充。
  40.                         this.fill(i, j);
  41.                 }
  42.         }
  43.         public static void main(String[] args) {
  44.                 FillArray obj = new FillArray(6);
  45.                 obj.fill(0, 0);
  46.                 int[][] array = obj.array;
  47.                 for(int[] row:array){
  48.                         for(int col:row){
  49.                                 System.out.print(col+" ");
  50.                         }
  51.                         System.out.println();
  52.                 }
  53.         }
  54. }
复制代码
只要还是递归。这个方阵看起来是一个 回 字。 所以先遍历外面的一圈口  然后再递归里面的几圈口。
这个感觉还是有点面对过程。谁能用面对对象把代码简化一下。

评分

参与人数 1技术分 +2 收起 理由
admin + 2

查看全部评分

回复 使用道具 举报
不知道上面这个代码可不可以再简洁点。感觉太长了。
回复 使用道具 举报
这个刚好我前段时间写过,可以自己控制螺旋大小!
public class Test8 {//螺旋矩阵
        public static void main(String[] args) {
                Scanner read=new Scanner(System.in);
                System.out.println("请输入矩阵大小:");
                int n=read.nextInt();
                f(n,1);
        }
        public static void f(int n,int k){
                if(n==1)
                        System.out.println(1);
                else{
                        int a[][]=new int[n][n];
                        char flag='R';
                        int i=0,j=0;
                        while(a[i][j]==0){
                                a[i][j]=k++;
                                switch(flag){
                                case 'R':
                                        if(j+1<a[i].length && a[i][j+1]==0)
                                                j++;
                                        else{
                                                i++;
                                                flag='D';
                                        }
                                        break;
                                case 'D':
                                        if(i<a.length-1 && a[i+1][j]==0)
                                                i++;
                                        else{
                                                j--;
                                                flag='L';
                                        }
                                        break;
                                case 'L':
                                        if(j>=1 && a[i][j-1]==0)
                                                j--;
                                        else{
                                                i--;
                                                flag='U';
                                        }
                                        break;
                                case 'U':
                                        if(i>=1 && a[i-1][j]==0)
                                                i--;
                                        else{
                                                j++;
                                                flag='R';
                                        }
                                }
                        }
                        print(a);
                }                       
        }
        //输出数组元素
        public static void print(int a[][]){
                for(int i=0;i<a.length;i++){
                        for(int j=0;j<a.length;j++){
                                if(a[i][j]<10)
                                    System.out.print("0"+a[i][j]+" ");
                                else
                                        System.out.print(a[i][j]+" ");
                        }
                        System.out.println();
                }
        }
}
回复 使用道具 举报
public class Test {
public static int[][] makeMatrix(int w, int h) {
   int[][] mtr = new int[w][h];
   int d = 1, x = 0, y = 0;
   while(true) {
     mtr[x][y] = d++;
     //各方向可否前进
     boolean right = x< w-1&&mtr[x+1][y]==0;
     boolean down = y< h-1&&mtr[x][y+1]==0;
     boolean left = x>0&&mtr[x-1][y]==0;
     boolean up = y>0&&mtr[x][y-1]==0;
     //判断前进方向
     if(right) if(up) y--; else x++;
     else if(down) if(right) x++; else y++;
     else if(left) if(down) y--; else x--;
     else if(up) if(left) x--; else y--;
     else break;
   }
   return mtr;
}
  
public static void printMatrix(int[][] mtr) { //输出
   int w = mtr.length;
   int h = mtr[0].length;
   for(int i=0; i< h; i++) {
     for(int j=0; j< w; j++) {
       System.out.print(mtr[j][i] + "\t");
     }
     System.out.println ();
   }
}
  
public static void main(String[] args) {
   int h = Integer.parseInt(args[0]);
   int w = Integer.parseInt(args[1]);
   printMatrix(makeMatrix(h,w));
}
}
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马