/** Print the nine cells */ # Include <stdio. h> # Include <assert. h> Int main (){ Printf ("enter an odd number :"); Int n; Scanf ("% d", & n ); // Asserted to determine whether a condition is true. If not, the program exits. Assert (n % 2! = 0 ); Int nine [n] [n]; // Save the array of the nine Cells For (int I = 0; I <n; I ++ ){ For (int j = 0; j <n; j ++ ){ Nine [I] [j] = 0; } } // Place I in the place of the loop Int row = 0; // The row. The first row is the top row. Int col = n/2; // column, initially the middle column For (int I = 1; I <= n * n; I ++ ){ Nine [row] [col] = I; // Calculate the row and column to be placed in the next number // Go up at 45 degrees Row --; Col ++; // If they all cross the border, put them below the original If (row <0 & col = n ){ Row + = 2; Col --; } // If the row is out of bounds, put it at the bottom Else if (row <0 ){ Row = n-1; } // If the column is out of bounds, place it on the leftmost side. Else if (col = n ){ Col = 0; } // If the conflict exists, put the following: Else if (nine [row] [col]! = 0 ){ Row + = 2; Col --; } } // Print For (int I = 0; I <n; I ++ ){ For (int j = 0; j <n; j ++ ){ Printf ("% d", nine [I] [j]); } Printf ("\ n "); } Return 0; } |