62,634
社区成员




package com.csdn;
public class TestRec {
public static final int MIN = 2;
public static final int MAX = 9;
enum direction {
up, down, left, right
}
public static void main(String[] args) {
int n = 4; // 这里可以自行输入
int number, number_max;
int x, y;
int boundary_top, boundary_bottom, boundary_left, boundary_right;
int[][] matrix = new int[MAX][MAX];
direction current_direction = direction.right;
if ((n < MIN) || (n > MAX))
return;
boundary_top = 0;
boundary_bottom = n - 1;
boundary_left = 0;
boundary_right = n - 1;
x = 0;
y = 0;
number_max = n * n;
for (number = 1; number <= number_max; number++) {
matrix[x][y] = number;
switch (current_direction) {
case right:
if (++x == boundary_right) {
current_direction = direction.down;
boundary_top++;
}
break;
case down:
if (++y == boundary_bottom) {
current_direction = direction.left;
boundary_right--;
}
break;
case left:
if (--x == boundary_left) {
current_direction = direction.up;
boundary_bottom--;
}
break;
case up:
if (--y == boundary_top) {
current_direction = direction.right;
boundary_left++;
}
break;
default:
break;
}
}
for (y = 0; y < n; y++) {
for (x = 0; x < n; x++) {
System.out.printf("%5d", matrix[x][y]);
}
System.out.printf("\n");
}
}
}
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
public class Test {
public static void main(String[] args) {
test(6);
}
public static void test(int num){
int start = - (num - 1) / 2;
int end = num / 2;
int max;
int maxNum;
for(int i = start; i <= end; i++){
for(int j = start; j <= end; j++){
max = Math.max(Math.abs(i), Math.abs(j));
maxNum = (2 * max + 1) * (2 * max + 1);
if(i == - max){
System.out.print(maxNum - max + j);
}else if(j == - max){
System.out.print(maxNum - 3*max - i);
}else if(i == max){
System.out.print(maxNum - 5*max - j);
}else{
System.out.print(maxNum - 7*max + i);
}
System.out.print("\t");
}
System.out.println();
System.out.println();
}
}
}
测试结果:
21 22 23 24 25 26
20 7 8 9 10 27
19 6 1 2 11 28
18 5 4 3 12 29
17 16 15 14 13 30
36 35 34 33 32 31
public class RoundArray {
public static int[][] createRoundArray(int n) {
int[][] roundArray = new int[n][n];
int[] add = { 1, 0 };
int size = n*n;
int x = 0;
int y = 0;
for (int i = 1; i <= size; i++) {
roundArray[x][y] = i;
int row = x + add[0];
int column = y + add[1];
if (row >= 0 && row < n && column >= 0 && column < n
&& roundArray[row][column] == 0) {
x = row;
y = column;
} else {
if (add[0] == 1) {
add[0] = 0;
add[1] = 1;
y = y + 1;
} else if (add[1] == 1) {
add[0] = -1;
add[1] = 0;
x = x - 1;
} else if (add[0] == -1) {
add[0] = 0;
add[1] = -1;
y = y - 1;
} else if (add[1] == -1) {
add[0] = 1;
add[1] = 0;
x = x + 1;
}
}
}
return roundArray;
}
public static void main(String[] args) {
int[][] array = RoundArray.createRoundArray(12);
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.printf("%3d",array[i][j]);
System.out.print(" ");
}
System.out.println();
}
}
result:
1 44 43 42 41 40 39 38 37 36 35 34
2 45 80 79 78 77 76 75 74 73 72 33
3 46 81 108 107 106 105 104 103 102 71 32
4 47 82 109 128 127 126 125 124 101 70 31
5 48 83 110 129 140 139 138 123 100 69 30
6 49 84 111 130 141 144 137 122 99 68 29
7 50 85 112 131 142 143 136 121 98 67 28
8 51 86 113 132 133 134 135 120 97 66 27
9 52 87 114 115 116 117 118 119 96 65 26
10 53 88 89 90 91 92 93 94 95 64 25
11 54 55 56 57 58 59 60 61 62 63 24
12 13 14 15 16 17 18 19 20 21 22 23