连续数字的旋转二维数组

生成一个连续数字的旋转二维数组,起始位置位于左上角,然后从起始位置开始递增,按照顺时针或者逆时针从外向里将连续的数组存入二维数组中,并输出生成的旋转二维数组。 

Java代码   收藏代码
  1. /** 
  2.  *  
  3.  * <code>ConvolutionalArray</code>主要是用来输出一个旋转式样的二维数组。 
  4.  *  
  5.  *  
  6.  * 此二维数组由连续的数字组成,比如: 
  7.  *  *  
  8.  * int i=5;    
  9.  * 1  2  3  4  5   
  10.  * 16 17 18 19 6   
  11.  * 15 24 25 20 7   
  12.  * 14 23 22 21 8   
  13.  * 13 12 11 10 9   
  14.   
  15.  * int i=6   
  16.  * 1  2  3  4  5   6   
  17.  * 20 21 22 23 24  7   
  18.  * 19 32 33 34 25  8   
  19.  * 18 31 36 35 26  9   
  20.  * 17 30 29 28 27 10   
  21.  * 16 15 14 13 12 11   
  22.  *  
  23.  
  24.  *  
  25.  * @author Eric 
  26.  * @version 1.0 
  27.  *  
  28.  */  
  29.   
  30. public class ConvolutionalArray {  
  31.   
  32.     private static final int DEFAULT_NUMBER = 5;  
  33.   
  34.     // 定义连续数字添加的方向  
  35.     private enum Direction {  
  36.         Right, Down, Left, Up;  
  37.     }  
  38.   
  39.     // 保存当前处理的方向,默认一开始是向右的。  
  40.     private Direction currentDirection = Direction.Right;  
  41.   
  42.     // 将一个二维数组看成X, Y两个方向,记录下,最小的X,Y值以及最大的X,Y  
  43.     private int minX = 0;  
  44.   
  45.     private int minY = 0;  
  46.   
  47.     private int maxX = DEFAULT_NUMBER - 1;  
  48.   
  49.     private int maxY = DEFAULT_NUMBER - 1;  
  50.   
  51.     private int currentCount = 0;  
  52.   
  53.     /** 
  54.      * 打印数组。 
  55.      *  
  56.      * @param expectedNumber : 
  57.      *            期望的二维数组每行显示的个数。 
  58.      */  
  59.     public void printArray(int expectedNumber) {  
  60.         int[][] array = buildArray(expectedNumber);  
  61.         printArray(array, expectedNumber);  
  62.     }  
  63.   
  64.     /** 
  65.      *  
  66.      * @param expectedNumber: 
  67.      *            期望的二维数组每行显示的个数。 
  68.      * @return: 返回一个连续数字组成的二维数组。 
  69.      */  
  70.     private int[][] buildArray(int expectedNumber) {  
  71.         int[][] result = new int[expectedNumber][expectedNumber];  
  72.         // Total number of numeric values  
  73.         int count = expectedNumber * expectedNumber;  
  74.         // Reset max value for maxX and maxY.  
  75.         maxY = expectedNumber - 1;  
  76.         maxX = expectedNumber - 1;  
  77.         /* 
  78.          * 如果要生成一个nxn的二维数组,那么里面的最大数应该是n*n, 当处理的数字还没有达到这个最大值,一直循环处理下去。 
  79.          *  
  80.          */  
  81.         while (currentCount < count) {  
  82.             /* 
  83.              * 因为开始默认的方向是Right,也就是顺时针方向,所以,处理的顺序是 Right -> Down -> Left -> Up 
  84.              * ->Right ... 每次一个方向填充值之后,需要先判断当前数据是否达到最大值。 
  85.              */  
  86.             if (currentCount < count) {  
  87.                 processRight(result);  
  88.             }  
  89.             if (currentCount < count) {  
  90.                 processDown(result);  
  91.             }  
  92.             if (currentCount < count) {  
  93.                 processLeft(result);  
  94.             }  
  95.             if (currentCount < count) {  
  96.                 processUp(result);  
  97.             }  
  98.         }  
  99.         return result;  
  100.     }  
  101.   
  102.     /** 
  103.      * 向右方向填充数据。 
  104.      */  
  105.     private void processRight(int[][] currentArray) {  
  106.         for (int i = minX; i <= maxX; i++) {  
  107.             currentArray[minY][i] = ++currentCount;  
  108.         }  
  109.         changeDirection();  
  110.         minY++;  
  111.     }  
  112.   
  113.     /** 
  114.      * 向下方向填充数据。 
  115.      */  
  116.     private void processDown(int[][] currentArray) {  
  117.         for (int i = minY; i <= maxY; i++) {  
  118.             currentArray[i][maxX] = ++currentCount;  
  119.         }  
  120.         changeDirection();  
  121.         maxX--;  
  122.     }  
  123.   
  124.     /** 
  125.      * 向左方向填充数据。 
  126.      */  
  127.     private void processLeft(int[][] currentArray) {  
  128.         for (int i = maxX; i >= minX; i--) {  
  129.             currentArray[maxY][i] = ++currentCount;  
  130.         }  
  131.         changeDirection();  
  132.         maxY--;  
  133.     }  
  134.   
  135.     /** 
  136.      * 向上方向填充数据。 
  137.      */  
  138.     private void processUp(int[][] currentArray) {  
  139.         for (int i = maxY; i >= minY; i--) {  
  140.             currentArray[i][minX] = ++currentCount;  
  141.         }  
  142.         changeDirection();  
  143.         minX++;  
  144.     }  
  145.   
  146.     /** 
  147.      * 打印二维数组的内容。 
  148.      *  
  149.      * @param array 
  150.      * @param expectedNumber 
  151.      */  
  152.     private void printArray(int[][] array, int expectedNumber) {  
  153.         for (int i = 0; i < expectedNumber; i++) {  
  154.             for (int j = 0; j < expectedNumber; j++) {  
  155.                 System.out.print(appendSpace(array[i][j], expectedNumber));  
  156.             }  
  157.             System.out.println();  
  158.         }  
  159.     }  
  160.   
  161.     /** 
  162.      * 为了保证数据输出时能够对齐,添加一些空格. 
  163.      */  
  164.     private String appendSpace(int value, int expectedNumber) {  
  165.         int maxValue = expectedNumber * expectedNumber;  
  166.         int maxLength = String.valueOf(maxValue).length();  
  167.         int currentValueLength = String.valueOf(value).length();  
  168.         StringBuilder sb = new StringBuilder();  
  169.         sb.append(value);  
  170.         for (int i = maxLength; i > currentValueLength; i--) {  
  171.             sb.append(" ");  
  172.         }  
  173.         sb.append(" ");  
  174.         return sb.toString();  
  175.     }  
  176.   
  177.     /** 
  178.      * 改变当前的方向。 
  179.      */  
  180.     private void changeDirection() {  
  181.         switch (currentDirection) {  
  182.         case Right:  
  183.             currentDirection = Direction.Down;  
  184.             break;  
  185.         case Down:  
  186.             currentDirection = Direction.Left;  
  187.             break;  
  188.         case Left:  
  189.             currentDirection = Direction.Up;  
  190.             break;  
  191.         case Up:  
  192.             currentDirection = Direction.Right;  
  193.             break;  
  194.         }  
  195.     }  
  196.   
  197.     /** 
  198.       * 在一个二维数组基础上,以对角线为对称线,实现两侧数据的交换,并返回这个交换数据后的二维数组。 
  199.       *  
  200.       * 这样做有一个用处:比如已经有一个向右的二维旋转数组(向右): 
  201.       *   
  202.       1  2  3  4   
  203.       12 13 14 5   
  204.       11 16 15 6   
  205.       10 9  8  7   
  206.       采用此方法之后,可以得到一个向下方向的二维旋转数组(向下) 
  207.       1  12 11 10  
  208.       2  13 16 9   
  209.       3  14 15 8   
  210.       4  5  6  7   
  211.       */  
  212.     private int[][] exchangeArray(int[][] originalArray, int expectedNumber) {  
  213.         int[][] exchangedArray = new int[expectedNumber][expectedNumber];  
  214.         for (int i = 0; i < expectedNumber; i++) {  
  215.             for (int j = 0; j < expectedNumber; j++) {  
  216.                 exchangedArray[i][j] = originalArray[j][i];  
  217.             }  
  218.         }  
  219.         return exchangedArray;  
  220.     }  
  221.   
  222.     /** 
  223.      * 打印交换数据后的二维数组。 
  224.      */  
  225.     public void printExchangedArray(int expectedNumber) {  
  226.         int[][] array = buildArray(expectedNumber);  
  227.         printArray(exchangeArray(array, expectedNumber), expectedNumber);  
  228.     }  
  229. }  


测试代码: 

Java代码   收藏代码
  1. public class Test {  
  2.     public static void main(String[] args) {  
  3.         for (int i = 5; i < 11; i++) {  
  4.             System.out.println("Direction: Right; int = " + i);  
  5.             new ConvolutionalArray().printArray(i);  
  6.             System.out.println();  
  7.             System.out.println("Direction: Down; int = " + i);  
  8.             new ConvolutionalArray().printExchangedArray(i);  
  9.             System.out.println();  
  10.         }  
  11.     }  
  12. }  


输出结果: 

 

 

 


下面在显示一个大小为 34*34的旋转二维数组: 




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值