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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

牛少锋

初级黑马

  • 黑马币:0

  • 帖子:13

  • 精华:0

© 牛少锋 初级黑马   /  2012-7-19 09:34  /  1514 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

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

评分

参与人数 1黑马币 +15 收起 理由
蒋映辉 + 15 其实可以不用递归的 循环和数组做更快.

查看全部评分

6 个回复

倒序浏览
package test2;

public class FillArray {

    private int[][] array ;
    private int max ;
    private int currentNum;
   
    public FillArray(int len){
            this.array = new int[len][len];
            this.max = len*len;
    }
   
    public void fill(int i,int j){
            // 若数组还没有填充完毕。
            if(this.currentNum<this.max){
                    // 从左到右填充。
                    for(;j<this.array.length && this.array[i][j]==0;j++){
                            this.array[i][j]= ++this.currentNum;
                    }
                    // 调整位置。
                    i++ ;  j--;
                    // 从上到下填充。
                    for(;i<this.array.length && this.array[i][j]==0;i++){
                            this.array[i][j] = ++this.currentNum;
                    }
                    // 调整位置。
                    i--;  j--;
                    // 从右到左填充。
                    for(;j>=0 && this.array[i][j]==0;j--){
                            this.array[i][j] = ++this.currentNum;
                    }
                    // 调整位置。
                    j++;  i--;
                    // 从下到上填充。
                    for(;i>=0&&this.array[i][j] ==0;i--){
                            this.array[i][j] = ++this.currentNum;
                    }
                    // 调整位置。
                    i++;  j++;
                    
                    // 递归填充。
                    this.fill(i, j);
            }
    }
    public static void main(String[] args) {
            FillArray obj = new FillArray(4);
            obj.fill(0, 0);
            int[][] array = obj.array;
            for(int[] row:array){
                    for(int col:row){
                            System.out.print(col+"\t");
                    }
                    System.out.println();
            }
    }
}

评分

参与人数 1技术分 +1 收起 理由
韦念欣 + 1 赞一个!

查看全部评分

回复 使用道具 举报

import java.util.Scanner;
public class TestPrint
{
        //输入一个整数coun
        Scanner in = new Scanner(System.in);
        int coun  = in.nextInt();
        //定义二维数组
        int[][] squar = new int[coun][coun];

        //打印一个矩形
        /*
        *****
        *****
        *****
        *****
        */

        void print()
        {
                for (int i = 0; i < coun; i++)
                {
                        for (int j = 0; j < coun; j++)
                        {
                                System.out.print(squar[j] + "\t ");
                        }
                        System.out.println();
                }
        }
        
        //调整位置
        //从左往右,从上到下,从右到左,从下到上
        void generate()
        {
               
                int dx = 1, dy = 1, x = 0, y = 0, _x, _y;
                boolean goHorizon = true;
               
                //输入的值循环的次数coun*coun
                for (int i = 1; i <= (coun*coun); i++)
                {
                        squar[y][x] = i;

                        if (goHorizon)
                        {
                                _x = x + dx;
                                if (_x > (coun-1) || _x < 0 || squar[y][_x] != 0)
                                {
                                        goHorizon = false;
                                        dx = -dx;
                                        y += dy;
                                } else {
                                        x = _x;
                                }
                        } else
                        {
                                _y = y + dy;
                                if (_y > (coun-1) || _y < 0 || squar[_y][x] != 0) {
                                        goHorizon = true;
                                        dy = -dy;
                                        x += dx;
                                } else {
                                        y = _y;
                                }
                        }
                }
        }

        public static void main(String[] args)
        {
                TestPrint test = new TestPrint();
                test.generate();
                test.print();
        }
}



打印出结果:




评分

参与人数 1技术分 +1 收起 理由
蒋映辉 + 1

查看全部评分

回复 使用道具 举报
public class PrintArray {
        public static void main(String[] args) {
                printArray(1);
                printArray(2);
                printArray(3);
                printArray(4);
                printArray(5);
                printArray(6);
                printArray(11);
        }
        public static void printArray(int number){
                if(number<=0){
                        throw new IllegalArgumentException(number+"小于等于0");
                }
                int[][] arrs=new int[number][number];
                for(int row=0,col=0,count=0,skip=0;count!=number*number;){
                        if(row==skip){
                                if(col<number-skip){
                                        arrs[row][col++]=++count;
                                        continue;
                                }
                                row++;
                                col--;
                        }
                        if(col==number-1-skip){
                                if(row<number-skip){
                                        arrs[row++][col]=++count;
                                        continue;
                                }
                                row--;
                                col--;
                        }
                        if(row==number-1-skip){
                                if(col>=skip){
                                        arrs[row][col--]=++count;
                                        continue;
                                }
                                col++;
                                row--;
                        }
                        if(col==skip){
                                if(row>skip){
                                        arrs[row--][col]=++count;
                                        if(row!=skip){
                                                continue;
                                        }
                                }
                                row++;
                                col++;
                        }
                        skip++;
                }
               
                System.out.println("number="+number);
                for(int row=0;row<number;row++){
                        for(int col=0;col<number;col++){
                                System.out.print(arrs[row][col]+"\t");
                        }
                        System.out.println();
                }
        }
}
回复 使用道具 举报
本帖最后由 rslheima 于 2012-7-19 15:40 编辑

这试题,该自己写的好!!对你有好处!!
回复 使用道具 举报
  1. package com.heima.test7;

  2. public class Test7 {
  3.         public static void main(String[] args){
  4.                 spiralMatrix(4);
  5.         }
  6.         public static void spiralMatrix(int n){
  7.                 int[][] arr= new int[n][n];
  8.                 int count =0;
  9.                 int row =0, col=0;
  10.                 Direction dir = Direction.RIGHT;
  11.                 int circle =1;
  12.                 while(true){
  13.                         count++;
  14.                         arr[row][col]=count;
  15.                         switch(dir){
  16.                         case RIGHT:
  17.                                 if(col<n-circle)
  18.                                         col++;
  19.                                 else{
  20.                                         dir=Direction.DOWN;
  21.                                         row++;
  22.                                 }
  23.                                 break;
  24.                         case DOWN:
  25.                                 if(row<n-circle)
  26.                                         row++;
  27.                                 else{
  28.                                         dir=Direction.LEFT;
  29.                                         col--;
  30.                                 }
  31.                                 break;
  32.                         case LEFT:
  33.                                 if(col>circle-1)
  34.                                         col--;
  35.                                 else{
  36.                                         dir=Direction.UP;
  37.                                         row--;
  38.                                 }
  39.                                 break;
  40.                         case UP:
  41.                                 if(row>circle)
  42.                                         row--;
  43.                                 else{
  44.                                         circle++;
  45.                                         dir =Direction.RIGHT;
  46.                                         col++;
  47.                                 }
  48.                                 break;
  49.                         }
  50.                         if(count>=n*n)
  51.                                 break;

  52.                 }
  53.                 for(int i=0;i<n;i++){
  54.                         for(int j=0;j<n;j++){
  55.                                 System.out.print(arr[i][j]+"\t");
  56.                         }
  57.                         System.out.println();
  58.                 }
  59.                 }
  60. }
复制代码
  1. package com.heima.test7;

  2. public enum Direction {
  3.         RIGHT,DOWN,LEFT,UP;
  4. }
复制代码
回复 使用道具 举报
  1. class demo{
  2.         public static void main(String[] args){
  3.                 method(9);
  4.         }
  5.        
  6.         public static void method(int x){
  7.                 int[][] arr = new int[x][x];
  8.                 int i, j, len=x>>1;
  9.                 arr[0][0] = 1;
  10.                 for (i=1; i<(x+1)/2; i++)
  11.                         arr[i][i] = arr[i-1][i-1] + 4*(x-i*2+1);

  12.                 for (i=0; i<len; i++){
  13.                         for (j=i+1; j<x-i; j++)
  14.                                 arr[i][j] = arr[i][j-1]+1;
  15.                 }
  16.                
  17.                 for (j=x-1; j>=len; j--){
  18.                         for (i=x-j; i<=j; i++)
  19.                                 arr[i][j] = arr[i-1][j]+1;
  20.                 }
  21.                
  22.                 for (i=x-1; i>=len; i--){
  23.                         for (j=i-1; j>=x-i-1; j--)
  24.                                 arr[i][j] = arr[i][j+1]+1;
  25.                 }
  26.                
  27.                 for (j=0; j<len; j++){
  28.                         for (i=x-j-2; i>=j+1; i--)
  29.                                 arr[i][j] = arr[i+1][j]+1;
  30.                 }
  31.                
  32.                 for (i=0; i<x; i++){
  33.                         for (j=0; j<x; j++){
  34.                                 System.out.print(arr[i][j]+"\t");
  35.                         }
  36.                         System.out.println();
  37.                 }
  38.         }
  39. }
复制代码
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马