java编程题1) 显示螺旋方阵.螺旋方阵将1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列.
来源:学生作业帮 编辑:大师作文网作业帮 分类:综合作业 时间:2024/11/10 22:11:06
java编程题1) 显示螺旋方阵.螺旋方阵将1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列.
public class Cat {
/**
* @param args
*/
public static void main(String[] args) {
int number = 5;
int[][] arys = printCircle(number);
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
System.out.print(arys[i][j] + "\t");
}
System.out.println();
}
}
// 1 2 3 4 5
// 16 17 18 19 6
// 15 24 25 20 7
// 14 23 22 21 8
// 13 12 11 10 9
private static int[][] printCircle(int number) {
int[][] ary = new int[number][number];
int rightLen;
int downLen;
int leftLen;
int upLen;
int rightRow = 0;
int rightCol = 0;
int leftRow = number - 1;
int leftCol = number - 1;
int downRow = 1;
int downCol = number - 1;
int upRow = number - 2;
int upCol = 0;
int digit = 1;
while(number > 0){
rightLen = number;
downLen = number - 1;
leftLen = number - 1;
upLen = number - 2;
//Step 1:print to right
for(int i = 0,temp = rightCol; i < rightLen; i++){
ary[rightRow][temp++] = digit++;
}
//Step 2:print to down
for(int j = 0,temp = downRow; j < downLen; j++){
ary[temp++][downCol] = digit++;
}
//move left
for(int m = 0,temp = leftCol - 1; m < leftLen; m++){
ary[leftRow][temp--] = digit++;
}
//move up
for(int n = 0,temp = upRow; n < upLen; n++){
ary[temp--][upCol] = digit++;
}
number = number - 2;
//increas/decrease the row and column
rightRow++;
rightCol++;
downRow++;
downCol--;
leftRow--;
leftCol--;
upRow--;
upCol++;
}
return ary;
}
}
-----------------for 8 --------------
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
/**
* @param args
*/
public static void main(String[] args) {
int number = 5;
int[][] arys = printCircle(number);
for (int i = 0; i < number; i++) {
for (int j = 0; j < number; j++) {
System.out.print(arys[i][j] + "\t");
}
System.out.println();
}
}
// 1 2 3 4 5
// 16 17 18 19 6
// 15 24 25 20 7
// 14 23 22 21 8
// 13 12 11 10 9
private static int[][] printCircle(int number) {
int[][] ary = new int[number][number];
int rightLen;
int downLen;
int leftLen;
int upLen;
int rightRow = 0;
int rightCol = 0;
int leftRow = number - 1;
int leftCol = number - 1;
int downRow = 1;
int downCol = number - 1;
int upRow = number - 2;
int upCol = 0;
int digit = 1;
while(number > 0){
rightLen = number;
downLen = number - 1;
leftLen = number - 1;
upLen = number - 2;
//Step 1:print to right
for(int i = 0,temp = rightCol; i < rightLen; i++){
ary[rightRow][temp++] = digit++;
}
//Step 2:print to down
for(int j = 0,temp = downRow; j < downLen; j++){
ary[temp++][downCol] = digit++;
}
//move left
for(int m = 0,temp = leftCol - 1; m < leftLen; m++){
ary[leftRow][temp--] = digit++;
}
//move up
for(int n = 0,temp = upRow; n < upLen; n++){
ary[temp--][upCol] = digit++;
}
number = number - 2;
//increas/decrease the row and column
rightRow++;
rightCol++;
downRow++;
downCol--;
leftRow--;
leftCol--;
upRow--;
upCol++;
}
return ary;
}
}
-----------------for 8 --------------
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
java编程题1) 显示螺旋方阵.螺旋方阵将1开始的自然数由方阵的最外圈向内螺旋方式地顺序排列.
一个关于螺旋方阵的问题
pascal:螺旋方阵 SOS!
螺旋方阵(注:编程题库)
程序设计:螺旋方阵!建立如图所示的9*9阶的顺时针螺旋方阵
java程序问题?下面是一个 5*5 的螺旋方阵(顺时针方向旋转)编程输出 10*10(n
请高手赐教:求C++中构造螺旋方阵的程序.谢谢.(所谓螺旋方阵,见详细提问)
用c语言如何设计螺旋方阵
一道Java编程题构造一个n阶方阵,方阵的大小从键盘输入,方阵的最外一圈元素为1,以后往内依次为2,3,.n.例如:1
将连续的自然数1至36按如图方式排列成一个方阵
C语言螺旋方阵的问题①先把数组初始化为0.②使用双重循环实现要求的处理while(k
C语言螺旋方阵1 2 3 4 5 16 17 18 19 615 24 25 20 714 23 22 21 813 1