卷別
資料結構
選擇國立中央大學 2026 · 考試 · 第 37 題
考科資料結構
A magic square is an matrix of the integers from 1 to such that the sum of each row and column and the two major diagonals is the same. The following figure shows a magic square for the case where the common sum is 65.
| 15 | 8 | 1 | 24 | 17 |
|---|---|---|---|---|
| 16 | 14 | 7 | 5 | 23 |
| 22 | 20 | 13 | 6 | 4 |
| 3 | 21 | 19 | 12 | 10 |
| 9 | 2 | 25 | 18 | 11 |
Coxeter has given the following rule for generating a magic square when is odd:
- Put a one (
// 1.) in the middle box of the top row. - Go up (
// 2.1) and left (// 2.2) assigning numbers in increasing order to empty boxes. If your move causes you to jump off the square (that is, you go beyond the square's boundaries), figure out where you would be if you landed on a box on the opposite side of the square. Continue with this box. - If a box is occupied (
// 3.1), go down (// 3.2) instead of up and continue.
1 square[0][(n-1)/2]=1; // 1. Top row [0]; middle box [(n-1)/2]
2 /* i and j are current position */
3 i=0;
4 j=(n-1)/2;
5 for (count=2;count<=n*n;count++) {
6 row=(i-1<0)?(n-1):(i-1); // 2.1 up
7 column=(j-1<0)?(n-1):(j-1); // 2.2 left
8 if(square[row][column]) // 3.1 occupied
9 i=(++i)%n; // 3.2 down
10 else{
11 i=________ ; // 3.3 ________
12 j=(j-1<0)?(n-1):--j;
13 }
14 square[i][j]=count;
15 }
Fill the blank i=________; with correct code at the location annotated by //3.3.
- (A)
n - (B)
row - (C)
column - (D)
count - (E)
i%n