← 回題庫

卷別

資料結構

選擇

國立中央大學 2026 · 考試 · 第 37 題

A magic square is an n×nn \times n matrix of the integers from 1 to n2n^2 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 n=5n = 5 where the common sum is 65.

15812417
16147523
22201364
321191210
92251811

Coxeter has given the following rule for generating a magic square when nn is odd:

  1. Put a one (// 1.) in the middle box of the top row.
  2. 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.
  3. 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

標準解析

AI 生成