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

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 杜超 高级黑马   /  2013-4-25 13:32  /  2609 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 杜超 于 2013-4-25 20:29 编辑

写一方法,打印等长的二维数组,要求从1开始的自然数由方阵的最外圈向内螺旋方式地
       顺序排列。 如: n = 4 则打印:
      
     

1.jpg (3.44 KB, 下载次数: 22)

1.jpg

11 个回复

倒序浏览

  1. <P> public class Test30 {

  2. private static int l, r, u, d;// 定义上下左右边界
  3. private static int[][] a = new int[0][0];// 数组
  4. private static int y = 0, x = 0;// 初始化坐标
  5. private static int step, temp;// 步长,上一个数组的值</P>
  6. <P> private static void add(int y, int x) {
  7.   temp += step;
  8.   a[y][x] = temp;
  9. }</P>
  10. <P> private static void left() {
  11.   while (x > l)
  12.    add(y, --x);// 未遇到左边界
  13.   if (y > u) {// 遇到左边界且未遇到上边界
  14.    l++;
  15.    up();
  16.   }
  17. }</P>
  18. <P> private static void right() {
  19.   while (x < r)
  20.    add(y, ++x);// 未遇到右边界
  21.   if (y < d) {// 遇到右边界且未遇到下边界
  22.    r--;
  23.    down();
  24.   }
  25. }</P>
  26. <P> private static void up() {
  27.   while (y > u)
  28.    add(--y, x);// 未遇到上边界
  29.   if (x < r) {// 遇到上边界且未遇到右边界
  30.    u++;
  31.    right();
  32.   }
  33. }</P>
  34. <P> private static void down() {
  35.   while (y < d)
  36.    add(++y, x);// 未遇到下边界
  37.   if (x > l) {// 遇到下边界且未遇到左边界
  38.    d--;
  39.    left();
  40.   }
  41. }
  42. // 产生数组(起始值,步长,矩阵边长)
  43. public static void spiralArray(int start, int step, int length) {
  44.   Test30.step = step;
  45.   l = u = 0;
  46.   r = d = length - 1;
  47.   a = new int[length][length];
  48.   a[0][0] = temp = start;
  49.   up();
  50. }</P>
  51. <P> public static void spiralArray(int i) {
  52.   spiralArray(0, 1, i);
  53. }</P>
  54. <P> public static void print() {// 输出矩阵
  55.   for (int i = 0; i < a.length; i++) {
  56.    for (int j = 0; j < a.length; j++) {
  57.     System.out.print(a[i][j] + "\t");
  58.    }
  59.    System.out.println();
  60.   }
  61. }</P>
  62. <P> public static void main(String[] args) {
  63.   spiralArray(4);
  64.   print();
  65. }</P>
  66. <P>}</P>
复制代码
这是我在网上找的算法,看看能不能帮到你,好像从零开始的 , 你再自己的思路上加以改正就好了
回复 使用道具 举报
袁梦希 发表于 2013-4-25 14:01
这是我在网上找的算法,看看能不能帮到你,好像从零开始的 , 你再自己的思路上加以改正就好了
...

有点复杂,不太好理解,先消化消化,争取午夜12点之前搞定,多谢了
回复 使用道具 举报
都是写算法的题,不用非要在这种题上浪费时间。如果所有的算法题你都能懂,那么就不差这一道题{:soso_e129:}
回复 使用道具 举报
本帖最后由 刘胜寒 于 2013-4-25 16:53 编辑
  1. class Main
  2. {
  3.                 static int[] Dirx = new int[]{0,1,0,-1};
  4.                 static int[] Diry = new int[]{1,0,-1,0};
  5.         public static void main(String[] args)
  6.         {
  7.                
  8.                
  9.                 int n =5;
  10.                 int[][] arr = new int[5][5];
  11.                 arr[0][0]=1;
  12.                 F(0,0,n,arr);
  13.                 for(int i=0;i<n;i++)
  14.                 {
  15.                         for(int j=0;j<n;j++)
  16.                                 System.out.print(arr[i][j]+"\t");
  17.                         System.out.println();
  18.                 }
  19.         }
  20.         public static void F(int x,int y, int n,int[][] arr)
  21.         {
  22.                 int row,col;
  23.                 for(int i=0;i<4;i++)
  24.                 {
  25.                         row = x+Dirx[i];
  26.                         col = y+Diry[i];
  27.                         if(row<0||row>=n||col<0||col>=n) continue;
  28.                         if(arr[row][col]!=0)continue;
  29.                         arr[row][col]=arr[x][y]+1;
  30.                         F(row,col,n,arr);
  31.                 }
  32.         }
  33. }
复制代码
我用递归给你写的
估计你能看的懂
Dirx 和Diry 是确定下一步是向上还是下是左还是右
代码不对



回复 使用道具 举报
就是这个图
回复 使用道具 举报
杜超 高级黑马 2013-4-25 16:42:58
7#
袁梦希 发表于 2013-4-25 16:08
都是写算法的题,不用非要在这种题上浪费时间。如果所有的算法题你都能懂,那么就不差这一道题{:soso_e129: ...

最后一道了,明天中午前搞定是没问题,感觉自己的思维还可以的,只是多花点时间而已,哈哈
回复 使用道具 举报
杜超 高级黑马 2013-4-25 17:14:10
8#
刘胜寒 发表于 2013-4-25 16:40
就是这个图

你这个排序不太符合要求啊,要求是螺旋式排序


1.jpg (35.27 KB, 下载次数: 22)

1.jpg
回复 使用道具 举报
  1. class Main
  2. {
  3.                
  4.         public static void main(String[] args)
  5.         {
  6.                
  7.                
  8.                 int n =6;
  9.                 int[][] arr = new int[7][7];
  10.                 arr[0][0]=1;
  11.                 F(0,0,n,arr);
  12.                 for(int i=1;i<n;i++)
  13.                 {
  14.                         if(arr[i][i]!=0)continue;
  15.                         arr[i][i]= arr[i][i-1]+1;
  16.                         F(i,i,n,arr);
  17.                 }
  18.         
  19.                 for(int i=0;i<n;i++)
  20.                 {
  21.                         for(int j=0;j<n;j++)
  22.                                 System.out.print(arr[i][j]+"\t");
  23.                         System.out.println();
  24.                 }
  25.         }
  26.         public static void F(int x,int y, int n,int[][] arr)
  27.         {
  28.                 int TX ,Ty = y;
  29.                 for(y=y+1;y<n;y++)//y
  30.                 {
  31.                         if(y>=n)break;
  32.                         if(arr[x][y]!=0)break;
  33.                         else arr[x][y] = arr[x][y-1]+1;
  34.                 }
  35.                
  36.                 for(x=x+1;x<n;x++)//x
  37.                 {
  38.                         if(x>=n)break;
  39.                         if(arr[x][y-1]!=0)break;
  40.                         else arr[x][y-1] = arr[x-1][y-1]+1;
  41.                 }
  42.                 System.out.println(x+":::"+y);
  43.                
  44.                 for(y=y-2;y>=0;y--)//y
  45.                 {
  46.                         if(arr[x-1][y]!=0)break;
  47.                         else arr[x-1][y] = arr[x-1][y+1]+1;
  48.                 }
  49.                
  50.                 for(x=x-2;x>=0;x--)//x
  51.                 {
  52.                         if(arr[x][Ty]!=0)break;
  53.                         else arr[x][Ty] = arr[x+1][Ty]+1;
  54.                 }
  55.                
  56.                
  57.         }
  58. }
复制代码
给你写好了....只要改变n的值就行
回复 使用道具 举报
对不对.....

VH4QL_T[@2Z9P_83E6LVWKD.jpg (18.79 KB, 下载次数: 17)

VH4QL_T[@2Z9P_83E6LVWKD.jpg
回复 使用道具 举报
刘胜寒 发表于 2013-4-25 17:31
对不对.....

对,就是这顺序,还是用递归吗,把代码页发过来吧我研究研究
回复 使用道具 举报
刘胜寒 发表于 2013-4-25 17:31
对不对.....

看到代码了,我先研究研究,谢谢了
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马