例如:输入 3 , 3
成都创新互联公司长期为上千客户提供的网站建设服务,团队从业经验10年,关注不同地域、不同群体,并针对不同对象提供差异化的产品和服务;打造开放共赢平台,与合作伙伴共同营造健康的互联网生态环境。为东港企业提供专业的网站建设、网站设计,东港网站改版等技术服务。拥有十余年丰富建站经验和众多成功案例,为您定制开发。
输出:
1 2 3
8 9 4
7 6 5
是一个顺时针的螺旋形输出。你看不懂的代码部分是在控制a[row][col] 的坐标,并对其依次赋值。
package cn点抗 .micc.javatwo; //根据实际情况修改
//蜗牛螺旋矩阵 请仔细研究矩阵阶数变化时数据的迁移规律
//上一阶矩阵会"整体"向右上或左下移动
public class AntiClockWiseArray {
public static int[][] getResult(int n) {
int[][] n1 = new int[1][1];
n1[0][0] = 1;
if (n == 1)
return n1;
int[][] result = new int[n][n];
int[][] temp = getResult(n - 1);
if (0 == (n - 1) % 2)
result = LeftDownMove(temp, n - 1); //n-1阶矩阵向左下移动
else
result = RightUpMove(temp, n - 1); //n-1阶矩阵向右上移动
return result;
}
public static int[][] LeftDownMove(int[][] in, int moment) {
int temp = moment * moment;
int nums = moment * 2 + 1;
int[][] out = new int[moment + 1][moment + 1];
//新矩阵补入上一阶矩阵的值
for (int i = 0; i moment; ++i)
for (int j = 0; j moment; ++j)
out[i + 1][j] = in[i][j];
//两个循环添加新矩阵新值
for (int k = 0; k moment + 1; ++k)
out[0][k] = temp + nums - k;
for (int l = 1; l moment + 1; ++l)
out[l][moment] = temp + nums - moment - l;
return out;
}
public static int[][] RightUpMove(int[][] in, int moment) {
int temp = moment * moment;
int nums = moment * 2 + 1;
int[][] out = new int[moment + 1][moment + 1];
//新矩阵补入上一阶矩阵的值
for (int i = 0; i moment; ++i)
for (int j = 0; j moment; ++j)
out[i][j + 1] = in[i][j];
//两个循环添加新矩阵新值
for (int k = 0; k moment + 1; ++k)
out[k][0] = temp + 1 + k;
for (int l = 1; l moment + 1; ++l)
out[moment][l] = temp + moment + 1 + l;
return out;
}
public static void printArray(int[][] temp, int n) {
//格式化打印矩阵
for(int i = 0; i n; ++i)
{
for(int j = 0; j n; ++j)
System.out.printf("%5d", temp[i][j]);
System.out.println();
}
}
public static void main(String[] args) {
printArray(getResult(6), 6); //输入阶数
}
}
output:
10阶
82 81 80 79 78 77 76 75 74 73
83 50 49 48 47 46 45 44 43 72
84 51 26 25 24 23 22 21 42 71
85 52 27 10 9 8 7 20 41 70
86 53 28 11 2 1 6 19 40 69
87 54 29 12 3 4 5 18 39 68
88 55 30 13 14 15 16 17 38 67
89 56 31 32 33 34 35 36 37 66
90 57 58 59 60 61 62 63 64 65
91 92 93 94 95 96 97 98 99100
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