例如:輸入 3 , 3
成都創(chuàng)新互聯(lián)公司長(zhǎng)期為上千客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為東港企業(yè)提供專業(yè)的網(wǎng)站建設(shè)、網(wǎng)站設(shè)計(jì),東港網(wǎng)站改版等技術(shù)服務(wù)。擁有十余年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。
輸出:
1 2 3
8 9 4
7 6 5
是一個(gè)順時(shí)針的螺旋形輸出。你看不懂的代碼部分是在控制a[row][col] 的坐標(biāo),并對(duì)其依次賦值。
package cn點(diǎn)抗 .micc.javatwo; //根據(jù)實(shí)際情況修改
//蝸牛螺旋矩陣 請(qǐng)仔細(xì)研究矩陣階數(shù)變化時(shí)數(shù)據(jù)的遷移規(guī)律
//上一階矩陣會(huì)"整體"向右上或左下移動(dòng)
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階矩陣向左下移動(dòng)
else
result = RightUpMove(temp, n - 1); //n-1階矩陣向右上移動(dòng)
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];
//新矩陣補(bǔ)入上一階矩陣的值
for (int i = 0; i moment; ++i)
for (int j = 0; j moment; ++j)
out[i + 1][j] = in[i][j];
//兩個(gè)循環(huán)添加新矩陣新值
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];
//新矩陣補(bǔ)入上一階矩陣的值
for (int i = 0; i moment; ++i)
for (int j = 0; j moment; ++j)
out[i][j + 1] = in[i][j];
//兩個(gè)循環(huán)添加新矩陣新值
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); //輸入階數(shù)
}
}
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