论坛上的一个帖子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
下面为程序代码
- /**
- *
- * @author hp9016
- * @version 1.0
- * 功能说明:实现回型数组的打印要求
- *
- */
- public class ForLouZhu {
- /**
- * 主函数
- */
- public static void main(String[] a){
- /*
- * n为总元素个数
- * 可以修改n的值,看看运行效果
- */
- int n = 24;
- new Array(n).print();
- }
- }
- /**
- *
- * @author hp9016
- * @version 1.0
- * 功能说明:存储行数和列数
- *
- */
- class Row {
- /*
- * 行数
- */
- private static int row;
- /*
- * 列数,即每行多少个元素
- */
- private static int length;
- public static int getLength() {
- return length;
- }
- public static void setLength(int length) {
- Row.length = length;
- }
- public static int getRow() {
- return row;
- }
- public static void setRow(int row) {
- Row.row = row;
- }
- }
- /**
- *
- * @author hp9016
- * @version 1.0
- * 功能说明:实现楼主要求
- *
- */
- class Array {
- /*
- * 一共有多少个元素,即二维数组的总元素数
- */
- private int n = 0;
- /*
- * 行数
- */
- private int row = 0;
- /*
- * 列数,即每行多少个元素
- */
- private int length = 0;
- /*
- * 欲打印的数组
- */
- private int[][] arr;
- /**
- * 构造方法
- * @param n 总元素个数
- */
- public Array(int n){
- this.n = n;
- }
- /**
- * 打印数组
- *
- */
- public void print(){
- /*
- * 填充数组
- */
- setArray();
- for(int i = 0; i< row; i++){
- for(int j = 0; j < length; j++){
- System.out.print(arr[i][j] + " ");
- }
- System.out.println();
- }
- }
- /**
- * 由总元素的数目n来决定数组的行数和列数,
- * n的最中间约数作为行数的行数和列数,比如:20
- * 则行数为4,列数为5;再比如24,
- * 则行数为4,列数为6,再比如25,
- * 则行数和列数均为5
- *
- */
- private void setRow(){
- int temp = (n + 1)/2;
- int length = n;
- for(int i = 1; i <= temp; i++){
- if(i >= length){
- /*
- * 存储到bean中
- */
- Row.setLength(length);
- Row.setRow(n / length);
- break;
- }
- if(n % i == 0){
- length = n / i;
- }
- }
- }
- /**
- * 核心函数
- * 按楼主要求--绕圈填充数组
- *
- */
- private void setArray(){
- /*
- * 临时行号
- */
- int tempRow = 0;
- /*
- * 临时列号
- */
- int tempLength = 0;
- /*
- * 当前最小没有被填充的行号
- */
- int minRow = 0;
- /*
- * 当前最小没有被填充的列号
- */
- int minLength = 0;
- /*
- * 当前最大没有被填充的行号
- */
- int maxRow = row;
- /*
- * 当前最大没有被填充的列号
- */
- int maxLength = length;
- /*
- * 记录总元素数目
- */
- int i =1;
- setRow();
- row = Row.getRow();
- length = Row.getLength();
- arr = new int[row][length];
- arr[0][0] = 1;
- maxRow = row - 1;
- maxLength = length - 1;
- /*
- * 本while的注释中的"最小"指:没有被填充的最小元素的下标
- * "最大"指:没有被填充的最大元素的下标
- * 填充的顺序如下:1>2>3>4
- * |--1--|
- * 4 2
- * |--3--|
- *
- */
- while(i < n){
- /*
- * 行号最小,列号最小时,类似于上图的1:
- * 从左向右这个方向填充数组
- */
- if(tempRow == minRow && tempLength == minLength ){
- /*
- * 设置最大列数
- */
- while(arr[tempRow][maxLength] != 0 &
- maxLength > 0){
- maxLength--;
- }
- /*
- * 从左向右这个方向填充数组
- */
- while(tempLength < maxLength){
- tempLength++;
- i++;
- arr[tempRow][tempLength] = i;
- }
- }
- /*
- * 行号最小,列号最大时,类似于上图的2:
- * 从上向下这个方向填充数组
- */
- if(tempRow == minRow && tempLength == maxLength ){
- /*
- * 设置最大行数
- */
- while(arr[maxRow][tempLength] != 0 &
- maxRow > 0){
- maxRow--;
- }
- /*
- * 从上向下这个方向填充数组
- */
- while(tempRow < maxRow){
- tempRow++;
- i++;
- arr[tempRow][tempLength] = i;
- }
- }
- /*
- * 行号最大,列号最大时,类似于上图的3:
- * 从右向左这个方向填充数组
- */
- if(tempRow == maxRow && tempLength == maxLength ){
- /*
- * 设置最小列数
- */
- while(arr[tempRow][minLength] != 0 &
- minLength < length - 1){
- minLength++;
- }
- /*
- * 从右向左这个方向填充数组
- */
- while(tempLength > minLength){
- tempLength--;
- i++;
- arr[tempRow][tempLength] = i;
- }
- }
- /*
- * 行号最大,列号最大时,类似于上图的4:
- * 从下向上这个方向填充数组
- */
- if(tempRow == maxRow && tempLength == minLength ){
- /*
- * 设置最小行数
- */
- while(arr[minRow][tempLength] != 0 &
- minRow < row -1){
- minRow++;
- }
- /*
- * 从下向上这个方向填充数组
- */
- while(tempRow > minRow){
- tempRow--;
- i++;
- arr[tempRow][tempLength] = i;
- }
- }
- }
- }
- }