java-51-输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。

  1. public class PrintMatrixClockwisely {   
  2.   
  3.     /**  
  4.      * Q51.输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字。  
  5. 例如:如果输入如下矩阵:  
  6.  
  7. 1              2             3            4  
  8. 5              6             7            8  
  9. 9              10           11           12  
  10. 13             14           15           16  
  11.  
  12. 则依次打印出数字1, 2, 3, 4, 8, 12, 16, 15, 14, 13, 9, 5, 6, 7, 11, 10。  
  13.      */  
  14.     public enum Direction{   
  15.         left,right,up,down,   
  16.     }   
  17.     public static void main(String[] args) {   
  18.          int[][]  matrix= {   
  19.                  { 12345},   
  20.                  { 6789,10},   
  21.                  {11,12,13,14,15},   
  22.                  {16,17,18,19,20},   
  23.                  {21,22,23,24,25},   
  24.                  };   
  25.          printMatrixClockwisely(matrix);   
  26.     }   
  27.   
  28.     /*  
  29.      * 思路比较直观,  
  30.      * 从左往右打印,到了右边界则向下,到了下边界就往左,到了左边界就往上。  
  31.      * 注意结束条件以及打印的时机  
  32.      */  
  33.     public static void printMatrixClockwisely(int[][] matrix){   
  34.   
  35.         int rowLen=matrix.length;   
  36.         int columnLen=matrix[0].length;   
  37.         Direction direction=Direction.right;   
  38.         int upBound=0;   
  39.         int downBound=rowLen-1;   
  40.         int leftBound=0;   
  41.         int rightBound=columnLen-1;   
  42.         int row=0;   
  43.         int column=0;   
  44.         while(true){   
  45.             System.out.print(matrix[row][column]+" ");   
  46.             //2 3 4 5 10 15 20 25 24 23 22 21 16 11 6 7 8 9 14 19 18 17 12 13   
  47.             if(upBound==downBound&&leftBound==rightBound){   
  48.                 break;   
  49.             }   
  50.             switch (direction){   
  51.             case right:   
  52.                 if(column<rightBound){   
  53.                     ++column;   
  54.                 }else{   
  55.                     ++row;   
  56.                     direction=Direction.down;   
  57.                     ++upBound;   
  58.                 }   
  59.                 break;   
  60.             case down:   
  61.                 if(row<downBound){   
  62.                     ++row;   
  63.                 }else{   
  64.                     --column;   
  65.                     direction=Direction.left;   
  66.                     --rightBound;   
  67.                 }   
  68.                 break;   
  69.             case up:   
  70.                 if(row>upBound){   
  71.                     --row;   
  72.                 }else{   
  73.                     ++column;   
  74.                     direction=Direction.right;   
  75.                     ++leftBound;   
  76.                 }   
  77.                 break;   
  78.             case left:   
  79.                 if(column>leftBound){   
  80.                     --column;   
  81.                 }else{   
  82.                     --row;   
  83.                     direction=Direction.up;   
  84.                     --downBound;   
  85.                 }   
  86.                 break;   
  87.             default:break;   
  88.             }   
  89.         }   
  90.            
  91.     }   
  92. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值