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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© quq947115876 中级黑马   /  2014-9-14 10:10  /  3477 人查看  /  11 人回复  /   1 人收藏 转载请遵从CC协议 禁止商业使用本文

9、 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列。
* 解题思路:
* 想象成一个人绕圈行走,所以要有方向,一个方向走到头,该方向的步数就要减少一步,直到循环足够退出。
* 1.循环时的四个边界
* 2.方向变换的条件
* 3.退出条件
  1. public class test9 {
  2.         //开始循环的数从1开始
  3.         public static int beginNum = 1;
  4.         //数组的大小
  5.         public static final int aryNum = 10;
  6.         //初始化行走方向,向左。  这里1表示向左,2表示向下,3表示向右,4表示向上
  7.         public static final int direction = 1;
  8.         public static void main(String[] args) {
  9.                
  10.                 int[][] num = new int[aryNum][aryNum];
  11.                 setNum(0, 0, 0, aryNum, 0, aryNum, direction, num);
  12.                 for(int a = 0;a<aryNum;a++){
  13.                         for(int b=0;b < aryNum;b++){
  14.                                 System.out.print(num[a][b]+"\t");
  15.                         }
  16.                         System.out.println("\n");
  17.                 }
  18.         }
  19.        
  20.         /*
  21.          * x:数组的一维坐标
  22.          * y:数组的二维坐标
  23.          * boundaryLeftY:数组的左边界,从0开始
  24.          * boundaryRightY:数组右边界,限制为数组的长度减一
  25.          * boundaryTopX:数组的上边界,从1开始,因为开始循环的时候最上方就已经占据了一行
  26.          * boundaryBottomX:数组的下边界,限制为数组的长度减一
  27.          * direction:行走的方向
  28.          * num:数组对象
  29.          */
  30.         public static void setNum(int x,int y,int boundaryLeftY,int boundaryRightY,int boundaryTopX,
  31.                         int boundaryBottomX,int direction,int[][] num){
  32.                         //当累加的量超过数组的平方大小就退出
  33.                         if(beginNum > aryNum*aryNum){
  34.                                 return;
  35.                         }
  36.                         System.out.println("x="+x+"  y="+y+"  direction="+direction+"  boundaryBottomX="+boundaryBottomX);
  37.                         switch (direction) {
  38.                         //向左方向
  39.                         case 1:
  40.                                 //不断向该方向填充数据
  41.                                 num[x][y++] = beginNum++;
  42.                                 //当y累加超过该方向的限制的时候就换方向
  43.                                 if(y > boundaryRightY - 1){
  44.                                         //y已经超过数据界限,所以必须对它重新赋值
  45.                                         y = boundaryRightY - 1;
  46.                                         //为下个方向累加
  47.                                         x++;
  48.                                         //换方向
  49.                                         setNum(x, y, boundaryLeftY, boundaryRightY - 1,boundaryTopX, boundaryBottomX,2, num);
  50.                                 }
  51.                                 //不需要变换方向
  52.                                 setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 1 , num);
  53.                                 break;
  54.                         //向下方向
  55.                         case 2:
  56.                                 num[x++][y] = beginNum++;
  57.                                 if(x > boundaryBottomX - 1){
  58.                                         x = boundaryBottomX - 1;
  59.                                         y--;
  60.                                         setNum(x, y, boundaryLeftY, boundaryRightY,boundaryTopX, boundaryBottomX - 1,3, num);
  61.                                 }
  62.                                 setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 2 , num);
  63.                                 break;
  64.                         //向右方向
  65.                         case 3:
  66.                                 num[x][y--] = beginNum++;
  67.                                 if(y < boundaryLeftY){
  68.                                         y = boundaryLeftY;
  69.                                         x--;
  70.                                         setNum(x, y, boundaryLeftY+1, boundaryRightY,boundaryTopX, boundaryBottomX,4, num);
  71.                                 }
  72.                                 setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 3 , num);
  73.                                 break;
  74.                         //向上方向
  75.                         case 4:
  76.                                 num[x--][y] = beginNum++;
  77.                                 if(x < boundaryTopX + 1){
  78.                                         x = boundaryTopX + 1;
  79.                                         y++;
  80.                                         setNum(x, y, boundaryLeftY, boundaryRightY,boundaryTopX+1, boundaryBottomX,1, num);
  81.                                 }
  82.                                 setNum(x, y, boundaryLeftY,boundaryRightY,boundaryTopX, boundaryBottomX, 4 , num);
  83.                                 break;
  84.                         default:
  85.                                 break;
  86.                         }
  87.         }
  88. }
复制代码


刚开始对这道题真是无从下手,想了好久,做这道题真的是觉得项目不是做出来,而是调试出来的。
代码中有不足之处请大家多提出来,互相学习。

11 个回复

倒序浏览
这是交通灯的?
回复 使用道具 举报
本帖最后由 hejinzhong 于 2014-9-14 11:19 编辑

  1. /*
  2. * 螺旋矩阵是指一个呈螺旋状的矩阵,它的数字由第一行开始到右边不断变大,向下变大,
  3. * 向左变大,向上变大,如此循环。
  4. */
  5. public class Circle {
  6.         /**
  7.          * 接收三个参数,矩阵的行和列,以及起始数字
  8.          */
  9.         public static void main(String[] args) {
  10.                 helix(4,4,1);
  11.         }
  12.         /*
  13.          * 这个是左上角开始,顺时针转的螺旋
  14.          *
  15.          * 思想:先给最外层一圈赋值,再给第二圈赋值,直至结束
  16.          * 循环次数由行和列中较小一个决定,即为大于--较小者一半的---最小整数
  17.          */
  18.         public static void helix(int w,int h,int start){
  19.                 int[][] arr = new int[w][h];
  20.                 int left=0,top=0,right=w-1,botton=h-1;
  21.                 int max = w>h?(h+1)/2:(w+1)/2;
  22.                 for(int i=0;i<max;i++){
  23.                         for(int x=left;x<=right;x++){
  24.                                 arr[top][x] = start++;
  25.                         }
  26.                         top++;

  27.                         for(int y=top;y<=botton;y++){
  28.                                 arr[y][right] = start++;
  29.                         }
  30.                         right--;

  31.                         for(int z=right;z>=left;z--){
  32.                                 arr[botton][z]=start++;
  33.                         }
  34.                         botton--;

  35.                         for(int k=botton;k>=top;k--){
  36.                                 arr[k][left]=start++;
  37.                         }
  38.                         left++;
  39.                 }
  40.                
  41.                 for(int i=0;i<w;i++){
  42.                         for(int j=0;j<h;j++){
  43.                                 System.out.print(arr[i][j]+"\t");
  44.                         }
  45.                         System.out.println();
  46.                 }
  47.         }
  48. }
复制代码

回复 使用道具 举报 1 0
package com.lianxi.test02;

import java.util.Scanner;

public class Test9 {

        /*
         * 写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈
         * 向内螺旋方式地顺序排列。
         */
        public static void main(String[] args) {
               
                Scanner scanner = new Scanner(System.in);
                int num = scanner.nextInt();
                method(num);
        }
       
        public static void method(int num)
        {
                int count = 0;
                int[][] arr = new int[num][num];
                int n = 0;
                if(num%2 == 0)
                        n = num/2;
                else
                        n = num/2 + 1;
               
                for(int i = 0;i<n;i++)
                {
                        for(int j = i;j<num-i;j++)
                        {
                                arr[i][j] = ++count;
                        }
                        for(int j = i+1;j<num-i;j++)
                        {
                                arr[j][num-1-i] = ++count;
                        }
                        for(int j = num-i-2;j>=i;j--)
                        {
                                arr[num-1-i][j] = ++count;
                        }
                        for(int j = num-i-2;j>=i+1;j--)
                        {
                                arr[j][i] = ++count;
                        }
                }
               
                for(int i = 0;i<num;i++)
                {
                        for(int j = 0;j<num;j++)
                        {
                                System.out.print(arr[i][j]+"\t");
                        }
                        System.out.println();
                }
        }

}
回复 使用道具 举报
学习学习
回复 使用道具 举报
略屌 还没仔细看 回头仔细研究下
回复 使用道具 举报
郑飞 高级黑马 2014-9-14 18:50:40
7#
呵呵 第一次做某些题目 确实很难 不过做一题会一类 加油
回复 使用道具 举报
向楼主学习啊~~
回复 使用道具 举报
用递归试试,没这么麻烦V
回复 使用道具 举报
不错继续坚持
回复 使用道具 举报
这道题原来如此啊
回复 使用道具 举报
好叼,感觉眼前一片黑
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马