要打印的矩形行列自定,方阵的话参数中 row = col public static void printVolutionArr(int row, int col){ int[][] map = new int[row][col]; int[][] directer = {{0,1},{1,0},{0,-1},{-1,0}}; int temp = 0; int x = 0, y = -1; for(int i = 1; i <= row * col; i++){ x += directer[temp][0]; y += directer[temp][1]; if(x < 0 || y < 0 || x >= row || y >= col || map[x][y] > 0){ x -= directer[temp][0]; y -= directer[temp][1]; temp = (temp + 1) % 4; x += directer[temp][0]; y += directer[temp][1]; } map[x][y] = i; } for (int i = 0; i < map.length; i++) { for (int j = 0; j < map[i].length; j++) { System.out.print(map[i][j] + "\t"); } System.out.println(); System.out.println(); } }
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 = 5, t = (N << 1) - 1; final int DIRECT = RIGHT_FIRST; int[][] matrix = new int[N][N]; int[] rc = { 0, 0 }; for(int c = 0, p = 0, n = 1; c < t; p = (c + 1) >> 1) { while(p++ < N) { matrix[rc[0]][rc[1]] = n++; if(p == N) { c++; } rc[(c & 1) ^ DIRECT] += 1 - (c & 2); } } print(matrix); } private static void print(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(); } } }
把代码整理了一下: public static void main(String[] args) { final int N = 5, t = (N << 1) - 1; final int DIRECT = RIGHT_FIRST; int[][] matrix = new int[N][N]; int[] rc = { 0, 0 }; int c = 0, n = 1; while(c < t) { int p = (c + 1) >> 1; while(p++ < N) { matrix[rc[0]][rc[1]] = n++; if(p == N) { c++; } rc[(c & 1) ^ DIRECT] += 1 - (c & 2); } } print(matrix); }
再建一个5X5(这个可以根据一维数组计算得到,我这里特指一下,为了方便描述算法)的数组,值都为。
在5X5的数组中,从最外层开始按顺时针填数据,遇到0就从一维数组中取数,填写进去,直到填满为止。
把5X5的数组自上而下打印出来,成了。
{
public static void main(String args[])
{
int n = 5; int a[][]=new int[n][n];
int x=n*n,z=0;
a[n/2][n/2]=x;
for(int k=0;k<=n-1;k+=2)
{
z=k/2-1;
for(int i=0;i<k;i++)
{
x--;
a[n/2-z+i][n/2-1-z]=x;
}
for(int j=0;j<k;j++)
{
x--;
a[n/2+1+z][n/2-z+j]=x;
}
for(int i=0;i<k;i++)
{
x--;
a[n/2+z-i][n/2+1+z]=x;
}
for(int j=0;j<k;j++)
{
x--;
a[n/2-1-z][n/2+z-j]=x;
}
}
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
{
System.out.printf("%d\t",a[i][j]);
if(j==n-1)
System.out.println();
}
}
}
可以把 n = 5;换成其他奇数;
先从a[0][0]到a[0][3] 对x轴加1 1 2 3 4
在从a[0]a[4]到a[3][4] 只对y轴加1 5 6 7 8
在从a[4][4] 到 a[0][1] 对x轴减1 9 10 11 12
在从a[0]a[0]到a[1][0] 只对y轴减1 13 14 15 16
然后再填充内侧 最后填充中间
import java.util.Scanner;
public class Rotate {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入方阵的阶数:");
int num = input.nextInt();
if(num < 1) {
System.out.println("请输入合适的方阵阶数!");
return;
}
int count = 1;
int[][] arr = new int[num][num];
int i=0,j=0;
//整个图形分为从外到内的一些正方形,如果是偶数的话,
//正好合适,若是奇数的话,则最里面的一个正方形比较特殊,
//只有一个数字组成,所以循环结束后特殊处理。下面的里层
//的四个for循环,分别控制每个正方形的四条边的数字的设
//置,按照顺时针的顺序
for(int k=0; k<num/2; k++) {
for(j=k; j<arr.length-k; j++) {
arr[i][j] = count++;
}
for(i++,j--; i<arr.length-k; i++) {
arr[i][j] = count++;
}
for(i--,j--; j>=k; j--) {
arr[i][j] = count++;
}
for(j++,i--; i>k; i--) {
arr[i][j] = count++;
}
i++;
}
//如果为奇数,则设置中间位置的值
if(num % 2 == 1) {
arr[num / 2][num / 2] = count;
}
//打印数组
//length为该数组的最大元素的长度,也就是输出的数字的宽度
int length = String.valueOf(count).length();
for(i=0; i<arr.length; i++) {
for(j=0; j<arr[i].length; j++) {
System.out.format("%" + length + "d ", arr[i][j]);
}
System.out.println();
}
}
}
要打印的矩形行列自定,方阵的话参数中 row = col
public static void printVolutionArr(int row, int col){
int[][] map = new int[row][col];
int[][] directer = {{0,1},{1,0},{0,-1},{-1,0}};
int temp = 0;
int x = 0, y = -1;
for(int i = 1; i <= row * col; i++){
x += directer[temp][0];
y += directer[temp][1];
if(x < 0 || y < 0 || x >= row || y >= col || map[x][y] > 0){
x -= directer[temp][0];
y -= directer[temp][1];
temp = (temp + 1) % 4;
x += directer[temp][0];
y += directer[temp][1];
}
map[x][y] = i;
}
for (int i = 0; i < map.length; i++) {
for (int j = 0; j < map[i].length; j++) {
System.out.print(map[i][j] + "\t");
}
System.out.println();
System.out.println();
}
}
public final static int RIGHT_FIRST = 1; public static void main(String[] args) { final int N = 5, t = (N << 1) - 1;
final int DIRECT = RIGHT_FIRST; int[][] matrix = new int[N][N];
int[] rc = { 0, 0 }; for(int c = 0, p = 0, n = 1; c < t; p = (c + 1) >> 1) {
while(p++ < N) {
matrix[rc[0]][rc[1]] = n++;
if(p == N) {
c++;
}
rc[(c & 1) ^ DIRECT] += 1 - (c & 2);
}
}
print(matrix);
} private static void print(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();
}
}
}
final int DIRECT = RIGHT_FIRST; int[][] matrix = new int[N][N];
int[] rc = { 0, 0 }; int c = 0, n = 1; while(c < t) {
int p = (c + 1) >> 1;
while(p++ < N) {
matrix[rc[0]][rc[1]] = n++;
if(p == N) {
c++;
}
rc[(c & 1) ^ DIRECT] += 1 - (c & 2);
}
}
print(matrix);
}