
#include iostream #include iomanip using namespace std; int main() { int n; cin n; int arr[10][10] {0}; // n10数组开10足够 int top 0, bottom n - 1; int left 0, right n - 1; int num 1; int maxNum n * n; while (num maxNum) { // 1. 顶行左→右 for (int j left; j right num maxNum; j) arr[top][j] num; top; // 2. 右列上→下 for (int i top; i bottom num maxNum; i) arr[i][right] num; right--; // 3. 底行右→左 for (int j right; j left num maxNum; j--) arr[bottom][j] num; bottom--; // 4. 左列下→上 for (int i bottom; i top num maxNum; i--) arr[i][left] num; left; } // 打印方阵每个数字占3格宽度 for (int i 0; i n; i) { for (int j 0; j n; j) { cout setw(3) arr[i][j]; } cout endl; } return 0; }