用Java写出回型数
什么是回型数
1 2 3 4 5 6 7 8
28 29 30 31 32 33 34 9
27 48 49 50 51 52 35 10
26 47 60 61 62 53 36 11
25 46 59 64 63 54 37 12
24 45 58 57 56 55 38 13
23 44 43 42 41 40 39 14
22 21 20 19 18 17 16 15
这样的数字就是回形数
用Java写出回型数
public static void main(String[] args) {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入回型数大小数值:");
int n = scanner.nextInt();
int[][] arr = new int[n][n];
int count = 0;//要显示的数量
int maxX = n -1; //x轴的最大下标
int maxY = n - 1; //y轴的最大下标
int minX = 0; //x轴的最小下标
int minY = 0; //y轴的最小下表
while (minX <= maxX) {
for (int x = minX; x <= maxX; x++) {
arr[minY][x] = ++count;
}
minY++;
for (int y = minY; y <= maxY; y++) {
arr[y][maxX] = ++count;
}
maxX--;
for (int x = maxX; x >= minX; x--) {
arr[maxY][x] = ++count;
}
maxY--;
for (int y = maxY; y >= minY; y--) {
arr[y][minX] = ++count;
}
minX++;
System.out.println(count);
}
for (int i = 0; i < arr.length; i++)
{
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
}
System.out.println();
}
}
}
}