62,616
社区成员
发帖
与我相关
我的任务
分享
public static void main(String args[]){
int constNum = 11;
int n=1;
//0 1 2 3 4 / 9 14 19 24 / 23 22 21 20 / 15 10 5 / 6 7 8 / 13 18 / 17 16 / 11 / 12
int [] test = new int[constNum*constNum];
int count = constNum;
boolean hasSub = true;
int []operator = new int[4];
operator[0] = 1;
operator[1] = constNum;
operator[2] = -1;
operator[3] = -constNum;
int index = 0;
int last = -1;
while (count > 0) {
for (int i = 0; i < count; i++) {
test[last + operator[index]] = n++;
last = last + operator[index];
}
index = (index +1)%4;
if(hasSub){
count--;
hasSub = false;
}
else{
hasSub = true;
}
}
for(int i=0;i<constNum*constNum;i++){
if(i%constNum==0){
System.out.println("");
}
System.out.print(test[i] + "\t");
}
}
public class Matrix {
private int[][] matrix;
private int n;
public Matrix(int n) {
this.n = n;
matrix = new int[n][n];
int[] direct = new int[] { 1, 0, -1, 0 };
int directX = 0, directY = 3, currentX = -1, currentY = 0;
for (int count = 0; count < n * n;) {
int nextX = currentX + direct[directX], nextY = currentY
+ direct[directY];
if (nextX >= n || nextY >= n || nextX < 0 || nextY < 0
|| matrix[nextY][nextX] != 0) {
directX = (directX + 1) % 4;
directY = (directY + 1) % 4;
continue;
}
matrix[nextY][nextX] = ++count;
currentX = nextX;
currentY = nextY;
}
}
public String toString() {
StringBuffer sb = new StringBuffer();
if (matrix != null) {
int length = new String(n * n + "").length();
String format = "%-" + length + "d ";
for (int[] array : matrix) {
for (int i : array) {
sb.append(String.format(format, i));
}
sb.append('\n');
}
}
sb.trimToSize();
return sb.toString();
}
}
public static int[][] aa(int k){
int[][] hui = new int[k][k];
int row=0;
int fx=0;
for(int i=1;i<k*k;){
if(fx%4==0){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[row][j]=i;
i++;
}
fx++;
}else if(fx%4==1){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[j][k-row-1]=i;
i++;
}
fx++;
}else if(fx%4==2){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[k-1-row][k-j-1]=i;
i++;
}
fx++;
}else if(fx%4==3){
for(int j=row;j<k-row-1&&i<=k*k;j++){
hui[k-j-1][row]=i;
i++;
}
fx++;
row++;
}
}
if(k%2==1){
hui[(int)(k/2)][(int)(k/2)]=k*k;
}else{
hui[k/2][k/2-1]=k*k;
}
return hui;
}