打印回型数组 回型矩阵 环形数组

来源:互联网 发布:iapp锁机源码大全 编辑:程序博客网 时间:2024/06/05 17:48

论坛上的一个帖子http://topic.csdn.net/u/20081202/13/834d7cb0-a063-4754-a702-612f3053bd60.html?seed=996406603

1    2  3  4  5
14  15  16  17  6
13  20  19  18  7
12  11  10  9  8

请问这样一个矩阵怎么打印出来啊

 

解决方法(适用范围:非素数的int型数组):


 由总元素的数目n来决定数组的行数和列数,
 n的最中间约数作为行数的行数和列数,比如:20
 则行数为4,列数为5;再比如24,
 则行数为4,列数为6,再比如25,
则行数和列数均为5

 

下面为程序代码

  1. /**
  2.  * 
  3.  * @author hp9016
  4.  * @version 1.0
  5.  * 功能说明:实现回型数组的打印要求
  6.  * 
  7.  */
  8. public class ForLouZhu {
  9.     /**
  10.      * 主函数
  11.      */
  12.     public static void main(String[] a){
  13.         
  14.         /*
  15.          * n为总元素个数
  16.          * 可以修改n的值,看看运行效果
  17.          */
  18.         int n = 24;
  19.         new Array(n).print();
  20.     }
  21. }
  22. /**
  23.  * 
  24.  * @author hp9016
  25.  * @version 1.0
  26.  * 功能说明:存储行数和列数
  27.  * 
  28.  */
  29. class Row {
  30.     
  31.     /*
  32.      * 行数
  33.      */
  34.     private static int row;
  35.     
  36.     /*
  37.      * 列数,即每行多少个元素
  38.      */
  39.     private static int length;
  40.     
  41.     public static int getLength() {
  42.         return length;
  43.     }
  44.     public static void setLength(int length) {
  45.         Row.length = length;
  46.     }
  47.     public static int getRow() {
  48.         return row;
  49.     }
  50.     public static void setRow(int row) {
  51.         Row.row = row;
  52.     }
  53. }
  54. /**
  55.  * 
  56.  * @author hp9016
  57.  * @version 1.0
  58.  * 功能说明:实现楼主要求
  59.  * 
  60.  */
  61. class Array {
  62.     
  63.     /*
  64.      * 一共有多少个元素,即二维数组的总元素数
  65.      */
  66.     private int n = 0;    
  67.     
  68.     /*
  69.      * 行数
  70.      */
  71.     private int row = 0;
  72.     
  73.     /*
  74.      * 列数,即每行多少个元素
  75.      */
  76.     private int length = 0;
  77.     
  78.     /*
  79.      * 欲打印的数组
  80.      */
  81.     private int[][] arr;
  82.     
  83.     /**
  84.      * 构造方法
  85.      * @param n 总元素个数
  86.      */
  87.     public Array(int n){
  88.         this.n = n;
  89.     }
  90.     
  91.     /**
  92.      * 打印数组
  93.      *
  94.      */
  95.     public void print(){
  96.         
  97.         /*
  98.          * 填充数组
  99.          */
  100.         setArray();
  101.         for(int i = 0; i< row; i++){
  102.             for(int j = 0; j < length; j++){
  103.                 System.out.print(arr[i][j] + "  ");
  104.             }
  105.             System.out.println();
  106.         }
  107.     }
  108.     
  109.     /**
  110.      * 由总元素的数目n来决定数组的行数和列数,
  111.      * n的最中间约数作为行数的行数和列数,比如:20
  112.      * 则行数为4,列数为5;再比如24,
  113.      * 则行数为4,列数为6,再比如25,
  114.      * 则行数和列数均为5
  115.      *
  116.      */
  117.     private void setRow(){
  118.         int temp = (n + 1)/2;
  119.         int length = n;
  120.         
  121.         for(int i = 1; i <= temp; i++){
  122.             
  123.             if(i >= length){
  124.                 
  125.                 /*
  126.                  * 存储到bean中
  127.                  */
  128.                 Row.setLength(length);
  129.                 Row.setRow(n / length);
  130.                 break;
  131.             }
  132.             
  133.             if(n % i == 0){
  134.                 length = n / i;
  135.             }
  136.         }
  137.     }
  138.     
  139.     /**
  140.      * 核心函数
  141.      * 按楼主要求--绕圈填充数组
  142.      *
  143.      */
  144.     private void setArray(){
  145.         
  146.         /*
  147.          * 临时行号
  148.          */
  149.         int tempRow = 0;
  150.         
  151.         /*
  152.          * 临时列号
  153.          */
  154.         int tempLength = 0;
  155.         
  156.         /*
  157.          * 当前最小没有被填充的行号
  158.          */
  159.         int minRow = 0;
  160.         
  161.         /*
  162.          * 当前最小没有被填充的列号
  163.          */
  164.         int minLength = 0;
  165.         
  166.         /*
  167.          * 当前最大没有被填充的行号
  168.          */
  169.         int maxRow = row;
  170.         
  171.         /*
  172.          * 当前最大没有被填充的列号
  173.          */
  174.         int maxLength = length;
  175.         
  176.         /*
  177.          * 记录总元素数目
  178.          */
  179.         int i =1;
  180.         
  181.         setRow();
  182.         row = Row.getRow();
  183.         length = Row.getLength();
  184.         arr = new int[row][length];
  185.         arr[0][0] = 1;
  186.         maxRow = row - 1;
  187.         maxLength = length - 1;
  188.         
  189.         /*
  190.          * 本while的注释中的"最小"指:没有被填充的最小元素的下标
  191.          * "最大"指:没有被填充的最大元素的下标
  192.          * 填充的顺序如下:1>2>3>4
  193.          * |--1--|
  194.          * 4     2
  195.          * |--3--|
  196.          * 
  197.          */
  198.         while(i < n){
  199.             
  200.             /*
  201.              * 行号最小,列号最小时,类似于上图的1:
  202.              * 从左向右这个方向填充数组
  203.              */
  204.             if(tempRow == minRow && tempLength == minLength ){
  205.                 
  206.                 /*
  207.                  * 设置最大列数
  208.                  */
  209.                 while(arr[tempRow][maxLength] != 0 &
  210.                         maxLength > 0){
  211.                     maxLength--;
  212.                 }
  213.                 
  214.                 /*
  215.                  * 从左向右这个方向填充数组
  216.                  */
  217.                 while(tempLength < maxLength){
  218.                     tempLength++;
  219.                     i++;
  220.                     arr[tempRow][tempLength] = i;
  221.                 }
  222.             }
  223.             
  224.             /*
  225.              * 行号最小,列号最大时,类似于上图的2:
  226.              * 从上向下这个方向填充数组
  227.              */
  228.             if(tempRow == minRow && tempLength == maxLength ){
  229.                 
  230.                 /*
  231.                  * 设置最大行数
  232.                  */
  233.                 while(arr[maxRow][tempLength] != 0 &
  234.                         maxRow > 0){
  235.                     maxRow--;
  236.                 }
  237.                 
  238.                 /*
  239.                  * 从上向下这个方向填充数组
  240.                  */
  241.                 while(tempRow < maxRow){
  242.                     tempRow++;
  243.                     i++;
  244.                     arr[tempRow][tempLength] = i;
  245.                 }
  246.             }
  247.             
  248.             /*
  249.              * 行号最大,列号最大时,类似于上图的3:
  250.              * 从右向左这个方向填充数组
  251.              */
  252.             if(tempRow == maxRow && tempLength == maxLength ){
  253.                 
  254.                 /*
  255.                  * 设置最小列数
  256.                  */
  257.                 while(arr[tempRow][minLength] != 0 &
  258.                         minLength < length - 1){
  259.                     minLength++;
  260.                 }
  261.                 
  262.                 /*
  263.                  * 从右向左这个方向填充数组
  264.                  */
  265.                 while(tempLength > minLength){
  266.                     tempLength--;
  267.                     i++;
  268.                     arr[tempRow][tempLength] = i;
  269.                 }
  270.             }
  271.             
  272.             /*
  273.              * 行号最大,列号最大时,类似于上图的4:
  274.              * 从下向上这个方向填充数组
  275.              */
  276.             if(tempRow == maxRow && tempLength == minLength ){
  277.                 
  278.                 /*
  279.                  * 设置最小行数
  280.                  */
  281.                 while(arr[minRow][tempLength] != 0 &
  282.                         minRow < row -1){
  283.                     minRow++;
  284.                 }
  285.                 
  286.                 /*
  287.                  * 从下向上这个方向填充数组
  288.                  */
  289.                 while(tempRow > minRow){
  290.                     tempRow--;
  291.                     i++;
  292.                     arr[tempRow][tempLength] = i;
  293.                 }
  294.             }
  295.         }
  296.     }
  297. }
原创粉丝点击