用Java寫出回型數(shù)
什么是回型數(shù)
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
這樣的數(shù)字就是回形數(shù)
用Java寫出回型數(shù)
public static void main(String[] args) {
while (true) {
Scanner scanner = new Scanner(System.in);
System.out.println("請(qǐng)輸入回型數(shù)大小數(shù)值:");
int n = scanner.nextInt();
int[][] arr = new int[n][n];
int count = 0;//要顯示的數(shù)量
int maxX = n -1; //x軸的最大下標(biāo)
int maxY = n - 1; //y軸的最大下標(biāo)
int minX = 0; //x軸的最小下標(biāo)
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();
}
}
}
}