黑马程序员技术交流社区
标题:
用java写一个螺旋方阵
[打印本页]
作者:
せR3n、何必装纯
时间:
2011-11-22 15:02
标题:
用java写一个螺旋方阵
用户输入一个数 就能打印出这个数的方阵
例如 用户输入4 打印结果就是
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
用递归和二维数组实现。
作者:
肖瑞军
时间:
2011-11-22 15:56
本帖最后由 肖瑞军 于 2011-11-22 16:03 编辑
public class ScrewSquares {
public static void main(String[] args) {
int w =4;
int h =4;
int[][] array = new int[w][h];
fill(array, 0, 0, w-1, h-1, 1);
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
System.out.printf("%2d ", array[x][y]);
}
System.out.println();
}
}
public static void fill(int[][] array, int left, int top, int right, int bottom, int start) {
if (left > right || top > bottom) return;
for (int x=left; x<=right; x++)
array[x][top] = start++;
for (int y=top+1; y<=bottom; y++)
array[right][y] = start++;
for (int x=right-1; x>=left; x--)
array[x][bottom] = start++;
for (int y=bottom-1; y>=top+1; y--)
array[left][y] = start++;
fill(array, left+1, top+1, right-1, bottom-1, start);//递归调用
}
}
作者:
袁世宇
时间:
2011-11-22 16:25
本帖最后由 袁世宇 于 2011-11-22 16:28 编辑
public class FillArray {
private int[][] array ;
private int max ;
private int currentNum;
public FillArray(int len){
this.array = new int[len][len];
this.max = len*len;
}
public void fill(int i,int j){
// 若数组还没有填充完毕。
if(this.currentNum<this.max){
// 从左到右填充。
for(;j<this.array.length && this.array
[j]==0;j++){
this.array
[j]= ++this.currentNum;
}
// 调整位置。
i++ ; j--;
// 从上到下填充。
for(;i<this.array.length && this.array
[j]==0;i++){
this.array
[j] = ++this.currentNum;
}
// 调整位置。
i--; j--;
// 从右到左填充。
for(;j>=0 && this.array
[j]==0;j--){
this.array
[j] = ++this.currentNum;
}
// 调整位置。
j++; i--;
// 从下到上填充。
for(;i>=0&&this.array
[j] ==0;i--){
this.array
[j] = ++this.currentNum;
}
// 调整位置。
i++; j++;
// 递归填充。
this.fill(i, j);
}
}
public static void main(String[] args) {
FillArray obj = new FillArray(6);
obj.fill(0, 0);
int[][] array = obj.array;
for(int[] row:array){
for(int col:row){
System.out.print(col+" ");
}
System.out.println();
}
}
}
作者:
袁世宇
时间:
2011-11-22 16:26
本帖最后由 袁世宇 于 2011-11-22 16:27 编辑
public class FillArray {
private int[][] array ;
private int max ;
private int currentNum;
public FillArray(int len){
this.array = new int[len][len];
this.max = len*len;
}
public void fill(int i,int j){
// 若数组还没有填充完毕。
if(this.currentNum<this.max){
// 从左到右填充。
for(;j<this.array.length && this.array[i][j]==0;j++){
this.array[i][j]= ++this.currentNum;
}
// 调整位置。
i++ ; j--;
// 从上到下填充。
for(;i<this.array.length && this.array[i][j]==0;i++){
this.array[i][j] = ++this.currentNum;
}
// 调整位置。
i--; j--;
// 从右到左填充。
for(;j>=0 && this.array[i][j]==0;j--){
this.array[i][j] = ++this.currentNum;
}
// 调整位置。
j++; i--;
// 从下到上填充。
for(;i>=0&&this.array[i][j] ==0;i--){
this.array[i][j] = ++this.currentNum;
}
// 调整位置。
i++; j++;
// 递归填充。
this.fill(i, j);
}
}
public static void main(String[] args) {
FillArray obj = new FillArray(6);
obj.fill(0, 0);
int[][] array = obj.array;
for(int[] row:array){
for(int col:row){
System.out.print(col+" ");
}
System.out.println();
}
}
}
复制代码
只要还是递归。这个方阵看起来是一个 回 字。 所以先遍历外面的一圈口 然后再递归里面的几圈口。
这个感觉还是有点面对过程。谁能用面对对象把代码简化一下。
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2