ackage com.datastructure.array;
import java.util.Scanner;
public class BackArray {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入整数(需要生成回形二维数组的行):");
int row = input.nextInt();
System.out.println("请输入整数(需要生成回形二维数组的列):");
int list = input.nextInt();
int[][] arr = new int[row][list];
int num = 1;
int mini = 0;
int maxi = row-1;
int minj = 0;
int maxj = list-1;
while(mini <= maxi && minj <= maxj) {
for (int x = minj; x <= maxj; x++) {
arr[mini][x] = num++;
}
mini++;
for (int y = mini; y <= maxi; y++) {
arr[y][maxj] = num++;
}
maxj--;
for (int x = maxj; x >= minj; x--) {
arr[maxi][x] = num++;
}
maxi--;
for (int y = maxi; y >= mini ; y--) {
arr[y][minj] = num++;
}
minj++;
}
for (int k = 0; k < arr.length; k++) {
for (int k2 = 0; k2 < arr.length; k2++) {
System.out.print(arr[k][k2] + " ");
}
System.out.println();
}
}
}
二维回形数组
最新推荐文章于 2023-12-05 19:42:05 发布