一道面试题引发的血案

在javaEye上看到一道面试题,挺有趣,自己做了一下(题目: http://www.javaeye.com/topic/545378
    要求打印出:
  1. int i=5;   
  2. 1  2  3  4  5  

  1. 16 17 18 19 6  
  2. 15 24 25 20 7  
  3. 14 23 22 21 8  
  4. 13 12 11 10 9  
  5.   
  6. int i=6  
  7. 1  2  3  4  5   6  
  8. 20 21 22 23 24  7  
  9. 19 32 33 34 25  8  
  10. 18 31 36 35 26  9  
  11. 17 30 29 28 27 10  
  12. 16 15 14 13 12 11 
 
下面是自己花2个多小时做的:
 
 
  1. /**  
  2.  * 在屏幕打印出  
  3.  * -----------------  
  4.  * i = 5  
  5.  * 1  2  3  4  5    
  6.  * 16 17 18 19 6    
  7.  * 15 24 25 20 7    
  8.  * 14 23 22 21 8    
  9.  * 13 12 11 10 9    
  10.  * -----------------  
  11.  * i=6    
  12.  * 1  2  3  4  5   6    
  13.  * 20 21 22 23 24  7   
  14.  * 19 32 33 34 25  8  
  15.  * 18 31 36 35 26  9   
  16.  * 17 30 29 28 27 10  
  17.  * 16 15 14 13 12 11   
  18.  * -----------------  
  19.  * i=7、8、···  
  20.  * 以此类推  
  21.  *   
  22.  * @author [email protected]  
  23.  * 2010-12-2  
  24.  */  
  25. public class CircleNumer {   
  26.        
  27.     private static int N_TIME = 0;   
  28.        
  29.     public CircleNumer(int n){   
  30.         this.N_TIME = n;   
  31.     }   
  32.        
  33.     /**  
  34.      * 存数组  
  35.      * @param currentVal 当前值  
  36.      * @param array 存放要打印的数字的数组  
  37.      * @param circleNum 第几圈,拐弯后变  
  38.      * @param desc 是否逆序  
  39.      * @param isLine true:行,false:列  
  40.      */  
  41.     private void setArray(int currentVal,int[][] array,int circleNum,boolean desc,boolean isLine){   
  42.            
  43.         //判断是否结束   
  44.         if(currentVal > N_TIME * N_TIME)   
  45.             return;   
  46.            
  47.         //顺序   
  48.         if(!desc){   
  49.             //从第count/2 - count/4个开始,到N_TIME - count/4结束,++   
  50.             int begin = circleNum/2 - circleNum/4;   
  51.             int end = N_TIME - circleNum/4;   
  52.             //行   
  53.             if(isLine){   
  54.                 //第几行   
  55.                 int line = (circleNum-1)/4;   
  56.                 for(int i = begin;i < end;i++)   
  57.                     array[line][i] = currentVal++;   
  58.             }else{   
  59.                 //第几列   
  60.                 int row = N_TIME - (circleNum-1)/4 -1;   
  61.                 for(int i = begin;i < end;i++)   
  62.                     array[i][row] = currentVal++;   
  63.             }   
  64.                
  65.                
  66.         }//逆序   
  67.         else{   
  68.             //从第N_TIME - (count/2 - count/4) - 1个开始,到count/4结束,--   
  69.             int begin = N_TIME - circleNum/2 + circleNum/4 - 1;   
  70.             int end = circleNum/4;   
  71.             if(isLine){   
  72.                 //第几行   
  73.                 int line = N_TIME - (circleNum-1)/4 -1;   
  74.                 for(int i = begin;i >= end;i--)   
  75.                     array[line][i] = currentVal++;   
  76.             }else{   
  77.                 //第几列   
  78.                 int row = (circleNum-1)/4;   
  79.                 for(int i = begin;i >= end;i--)   
  80.                     array[i][row] = currentVal++;   
  81.             }   
  82.         }   
  83.            
  84.         //count%2 == 0 改变desc   
  85.         if(circleNum%2 == 0)   
  86.             desc = !desc;   
  87.         circleNum++;   
  88.         setArray(currentVal,array,circleNum,desc,!isLine);   
  89.     }   
  90.        
  91.     //打印屏幕   
  92.     public void printCircle(){   
  93.            
  94.         int[][] array = new int[N_TIME][N_TIME];   
  95.         setArray(1,array,1,false,true);   
  96.         //加空格,为了美观   
  97.         int count = (int)Math.log10(N_TIME * N_TIME) + 1;   
  98.            
  99.         for(int i=0;i < N_TIME;i++)   
  100.             for(int j=0;j < N_TIME;j++){   
  101.                 //加空格,为了美观   
  102.                 int numCount = (int)Math.log10(array[i][j]) + 1;   
  103.                 System.out.print(array[i][j] + " ");   
  104.                 for(int k=0;k < (count - numCount);k++)   
  105.                     System.out.print(" ");   
  106.                 if(j == (N_TIME-1)) System.out.println();                      
  107.             }   
  108.     }   
  109.        
  110.        
  111.     public static void main(String[] args) {   
  112.            
  113.         CircleNumer cn = new CircleNumer(6);   
  114.         cn.printCircle();   
  115.            
  116.     }   
  117.   
  118. }  
 

 
学习别人怎么做的:
 
1、例子1
 
  1. public static void print(int count) {      
  2.         int is[][] = new int[count][count];      
  3.         int i = 0;      
  4.         int c = count * count;      
  5.         // 横向坐标累加器      
  6.         int j = 0;      
  7.         // 纵向坐标累加器      
  8.         int k = 0;      
  9.         // 横纵向控制,1为横向,-1为纵向      
  10.         int m = 1;      
  11.         // 坐标累加器,1为递增,-1为递减      
  12.         int n = 1;      
  13.         while (i < c) {      
  14.             is[j][k] = ++i;      
  15.             if (m > 0) {      
  16.                 k += n;      
  17.                 // 触边转向      
  18.                 if (k < 0 || k >= count || is[j][k] != 0) {      
  19.                     m *= -1;      
  20.                     k -= n;      
  21.                     j += n;      
  22.                 }      
  23.             } else {      
  24.                 j += n;      
  25.                 // 触边转向      
  26.                 if (j < 0 || j >= count || is[j][k] != 0) {      
  27.                     m *= -1;      
  28.                     j -= n;      
  29.                     n *= -1;      
  30.                     k += n;      
  31.                 }      
  32.             }      
  33.         }      
  34.          
  35.         for (int p = 0; p < count; ++p) {      
  36.             for (int q = 0; q < count; ++q)      
  37.                 System.out.print(is[p][q] + "\t");      
  38.             System.out.println();      
  39.         }      
  40.     }
 
2、例子2:
 
  1. class SnakePrint {    
  2.        
  3.     static int length = 7;      
  4.     static int value = 1;      
  5.     static int[][] snake = new int[length][length];      
  6.     static Direction lastDirection = Direction.Right;      
  7.     //枚举需要jdk1.5   
  8.     static enum Direction {      
  9.         Right, Down, Left, Up;   
  10.     }      
  11.      
  12.     public static void initialArray() {      
  13.         int row = 0, line = 0;      
  14.         for (int c = 0; c < length * length; c++) {      
  15.             snake[row][line] = value;      
  16.             lastDirection = findDirection(row, line);      
  17.             switch (lastDirection) {      
  18.                 case Right: line++; break;      
  19.                 case Down:  row++;  break;      
  20.                 case Left:  line--; break;      
  21.                 case Up:    row--;  break;      
  22.                 default: System.out.println("error");   
  23.             }      
  24.             value++;      
  25.         }      
  26.     }      
  27.      
  28.     static Direction findDirection(int row, int line) {      
  29.         Direction direction = lastDirection;      
  30.         switch (direction) {      
  31.             case Right:      
  32.                 if ((line == length - 1) || (snake[row][line + 1] != 0))      
  33.                     direction = direction.Down;      
  34.                 break;      
  35.             case Down:      
  36.                 if ((row == length - 1) || (snake[row + 1][line] != 0))      
  37.                     direction = direction.Left;      
  38.                 break;      
  39.             case Left:      
  40.                 if ((line == 0) || (snake[row][line - 1] != 0))      
  41.                     direction = direction.Up;      
  42.                 break;      
  43.             case Up:     
  44.                 if (snake[row - 1][line] != 0)      
  45.                     direction = direction.Right;      
  46.                 break;      
  47.         }      
  48.         return direction;      
  49.     }      
  50.      
  51.     public static void main(String[] args) {      
  52.         initialArray();      
  53.      
  54.         // display.....      
  55.         for (int i = 0; i < length; i++) {      
  56.             for (int j = 0; j < length; j++) {      
  57.                 System.out.print(snake[i][j] + "  ");      
  58.             }      
  59.             System.out.println();      
  60.         }      
  61.     }      
  62. }  

猜你喜欢

转载自wsqwsq000.iteye.com/blog/1120660