62,615
社区成员
发帖
与我相关
我的任务
分享
/**
*
* @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;
}
}
}
}
}
import java.util.Arrays;
/*这是实现类,构造方法的参数为一个row和一个coloum,形成一个矩阵*/
public class Cube {
int[] cube; //用于存放数据
int row; //行数
int coloum; //列数
public Cube(int row,int coloum){ //构造函数
/*初始化*/
this.cube = new int[row*coloum];
Arrays.fill(this.cube,0);
this.row = row;
this.coloum = coloum;
this.initialarry();
}
//形成队列
private void initialarry() {
int step = 1; //下一步要走的值
int id =0; //id号
int next; //存forward函数;
for(int i=1;i<=cube.length;i++){
cube[id] = i;
next = this.forward(id+1);
if(next!=0){ //如果不等于0
step=next;
}
id+=step; //确定下一步要要往哪里走
}
}
@Override
public String toString() {
String output=""; //输出
for(int i = 0;i<this.cube.length;i++){
output+=" "+String.valueOf(cube[i]);
if((i+1)%this.coloum==0){
output+="\n";
}
}
return output;
}
private int forward(int id){ //用于判断一个格子附近的4个格子是否为空
int count=0; //计数器
boolean[] forward = new boolean[4]; //表示方向
Arrays.fill(forward,false); //初始化
/*判断上面的格子*/
if((id-this.coloum)>0 //不是最上面一行
&&cube[id-this.coloum-1]==0){ //还没有赋值
count++;
forward[0] = true; //0表示向上
}
/*判断左面的格子*/
if((id-1)>0 //序列号正常
&&((id-1)%this.coloum)!=0 //不是最左面一列
&&cube[id-2]==0){
count++;
forward[1] = true; //1表示向左
}
/*判断右的格子*/
if((id+1)<=this.cube.length //序列号正常
&&(id%this.coloum)!=0 //不是最右面一列
&&cube[id]==0){
count++;
forward[2] = true; //2表示向右
}
/*判断下面的格子*/
if((id+this.coloum)<=this.cube.length //序列号正常
&&cube[id+this.coloum-1]==0){
count++;
forward[3] = true; //1表示向左
}
/*决定返回值*/
if(count!=1){ //根据观察,如果朋友不为一个的话,是不会转向的。
return 0;
/*如果为一时,那里那里为空,网哪里走*/
}else if(forward[0]){ //上面为空,往上走
return -this.coloum;
}else if(forward[1]){ //左面为空,往左走
return -1;
}else if (forward[2]){ //右面为空,往右走
return 1;
}else if(forward[3]){ //下面为空,往下走
return this.coloum;
}
return 0;
}
}
/*1 2 3 4 5
14 15 16 17 6
13 20 19 18 7
12 11 10 9 8
打印如上矩阵*/
public class MainApp {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Cube text = new Cube(5,5);
System.out.print(text);
}
}