int col = 4, row = 4, cirNums[] = new int[row * col]; int cir[]={1, 0, 0, 1, -1, 0, 0, -1}, cirInd = 0; int x = 0, y = 0; for(int i = 0; i < row * col; i++){ cirNums[x + y * col] = i + 1; x += cir[2 * cirInd]; y += cir[2 * cirInd + 1]; if(cir[2 * cirInd] < 0 && (x==0||cirNums[x + y * col-1]>0))cirInd = (cirInd + 1) % 4; else if(cir[2*cirInd] > 0 && (x == col - 1 || cirNums[x + y * col + 1] > 0))cirInd = (cirInd + 1) % 4; else if(cir[2 * cirInd + 1] < 0 && (y == 0 || cirNums[x + y * col - col] > 0))cirInd = (cirInd + 1) % 4; else if(cir[2 * cirInd + 1] > 0 && (y == row - 1 || cirNums[x + y * col + col] > 0))cirInd = (cirInd + 1) % 4; } for(int i = 0; i < row; i++){ for(int j = 0; j < col; j++) System.out.print(cirNums[j + i * col]+" "); System.out.println(); }
我也写了一个,拿来跟大家交流!import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.println("请输入行数:"); int n = input.nextInt(); int[][] num = new int[n][n];
public class SpiralMatrix { public final static int DOWN_FIRST = 0; public final static int RIGHT_FIRST = 1; public static void main(String[] args) { final int N = 4; final int DIRECT = RIGHT_FIRST; int[][] spiralMatrix = new int[N][N]; int[] rc = { 0, 0 }; int t = (N << 1) - 1; int c = 0; int num = 1; while(c < t) { int p = (c + 1) >> 1; while(p++ < N) { spiralMatrix[rc[0]][rc[1]] = num++; if(p == N) { c++; } rc[(c & 1) ^ DIRECT] += 1 - (c & 2); } } output(spiralMatrix); } private static void output(int[][] matrix) { for(int i = 0; i < matrix.length; i++) { for(int j = 0; j < matrix[i].length; j++) { if(j > 0) { System.out.print(' '); } System.out.printf("%2d", matrix[i][j]); } System.out.println(); } } }
有意思,我也来凑热闹。 思路是: 把填数的过程看成一个人在屋子里走,走过的地方和外边都不能去,每次贴着左手走,能拐弯就拐弯 呵呵,想迷宫算法。 不过我写的不精简 int n = 4; int result[][] = new int [n][n]; int x =0; int y=0; int value = 1;
int left = 1; int top = 2; int right = 4; int bottum = 8;
// 计算下一个位置,规则:靠左走。数组边界和有数字的地方都是墙 // 先看四周的四个点 int status = 0; status |= checkWall(x,y,-1, 0, left, result); status |= checkWall(x,y,0, -1, top, result); status |= checkWall(x,y,1, 0, right, result); status |= checkWall(x,y,0, 1, bottum, result); switch (status){ case 0: case 1: case 9: case 13: y--; break; case 2: case 3: case 11: x++; break; case 4: case 6: case 7: y++; break; case 8: case 12: case 14: x--; break; case 15: case 5: case 10: System.out.println("Oh no!"); break; } System.out.println("=="+status+"==>"+x+","+y); } for(int i=0;i<n*n;i++){ System.out.print(String.format("%-2d, ", result[i%n][i/n])); if (((i+1)%n) == 0) System.out.println(); }
int col = 4, row = 4, cirNums[] = new int[row * col];
int cir[]={1, 0, 0, 1, -1, 0, 0, -1}, cirInd = 0;
int x = 0, y = 0;
for(int i = 0; i < row * col; i++){
cirNums[x + y * col] = i + 1;
x += cir[2 * cirInd];
y += cir[2 * cirInd + 1];
if(cir[2 * cirInd] < 0 &&
(x==0||cirNums[x + y * col-1]>0))cirInd = (cirInd + 1) % 4;
else if(cir[2*cirInd] > 0 &&
(x == col - 1 || cirNums[x + y * col + 1] > 0))cirInd = (cirInd + 1) % 4;
else if(cir[2 * cirInd + 1] < 0 &&
(y == 0 || cirNums[x + y * col - col] > 0))cirInd = (cirInd + 1) % 4;
else if(cir[2 * cirInd + 1] > 0 &&
(y == row - 1 || cirNums[x + y * col + col] > 0))cirInd = (cirInd + 1) % 4;
}
for(int i = 0; i < row; i++){
for(int j = 0; j < col; j++)
System.out.print(cirNums[j + i * col]+" ");
System.out.println();
}
public class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入行数:");
int n = input.nextInt();
int[][] num = new int[n][n];
//辅助数组,其中的内容为1-n*n,用于给num依次赋值
int[] arr = new int[n*n];
for(int i=0; i<n*n; i++) {
arr[i] = i + 1;
}
//题目可以看成是由里向外一个一个输入正方形,总共循环n/2圈,
//n为偶数,循环结束正好合适;n为奇数,循环结束后,设置最后的一个值为n*n
int i = 0, j = 0;
int index = 0;
for(int count=0; count<n/2; count++) {
for(; j<n - count; j++) {
num[i][j] = arr[index ++];
}
for(--j,++i; i<n - count; i++) {
num[i][j] = arr[index ++];
}
for(--j,--i; j>count; j--) {
num[i][j] = arr[index ++];
}
for(; i>count; i--) {
num[i][j] = arr[index ++];
}
j++;
i++;
}
//判断n是否为奇数,为奇数就给最中心的一个赋值
if(n % 2 != 0) {
num[n / 2][n / 2] = n * n;
}
//按照格式打印出结果,格式串format是%3d这种样式,表示输出的数字占3位,右对齐
String format = "%" + (n * n + "").length() + "d ";
for(i=0; i<n; i++) {
for(j=0; j<n; j++) {
System.out.format(format,num[i][j]);
}
System.out.println();
}
}
}
public final static int RIGHT_FIRST = 1; public static void main(String[] args) { final int N = 4;
final int DIRECT = RIGHT_FIRST; int[][] spiralMatrix = new int[N][N];
int[] rc = { 0, 0 };
int t = (N << 1) - 1;
int c = 0;
int num = 1;
while(c < t) {
int p = (c + 1) >> 1;
while(p++ < N) {
spiralMatrix[rc[0]][rc[1]] = num++;
if(p == N) {
c++;
}
rc[(c & 1) ^ DIRECT] += 1 - (c & 2);
}
}
output(spiralMatrix);
} private static void output(int[][] matrix) {
for(int i = 0; i < matrix.length; i++) {
for(int j = 0; j < matrix[i].length; j++) {
if(j > 0) {
System.out.print(' ');
}
System.out.printf("%2d", matrix[i][j]);
}
System.out.println();
}
}
}
把填数的过程看成一个人在屋子里走,走过的地方和外边都不能去,每次贴着左手走,能拐弯就拐弯
呵呵,想迷宫算法。 不过我写的不精简 int n = 4;
int result[][] = new int [n][n];
int x =0;
int y=0;
int value = 1;
int left = 1;
int top = 2;
int right = 4;
int bottum = 8;
for(int i=0;i<n*n;i++){
System.out.print(x+","+y);
result[x][y] = value++; // 赋值
// 计算下一个位置,规则:靠左走。数组边界和有数字的地方都是墙
// 先看四周的四个点
int status = 0;
status |= checkWall(x,y,-1, 0, left, result);
status |= checkWall(x,y,0, -1, top, result);
status |= checkWall(x,y,1, 0, right, result);
status |= checkWall(x,y,0, 1, bottum, result);
switch (status){
case 0:
case 1:
case 9:
case 13:
y--;
break;
case 2:
case 3:
case 11:
x++;
break;
case 4:
case 6:
case 7:
y++;
break;
case 8:
case 12:
case 14:
x--;
break;
case 15:
case 5:
case 10:
System.out.println("Oh no!");
break;
}
System.out.println("=="+status+"==>"+x+","+y);
}
for(int i=0;i<n*n;i++){
System.out.print(String.format("%-2d, ", result[i%n][i/n]));
if (((i+1)%n) == 0)
System.out.println();
}