Run ID:130356

提交时间:2025-09-12 21:03:03

#include<bits/stdc++.h> using namespace std; int a[1001][1001]; int k = 1; int i = 0, j = 0; int d = 0; int n; int main() { cin >> n; while (k <= n * n) { a[i][j] = k++; if (d == 0) { if (j + 1 < n && a[i][j + 1] == 0) { j++; } else { d = 1; i++; } } else if (d == 1) { if (i + 1 < n && a[i + 1][j] == 0) { i++; } else { d = 2; j--; } } else if (d == 2) { if (j - 1 >= 0 && a[i][j - 1] == 0) { j--; } else { d = 3; i--; } } else if (d == 3) { if (i - 1 >= 0 && a[i - 1][j] == 0) { i--; } else { d = 0; j++; } } } for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { cout << setw(4) << a[i][j]; } cout << endl; } return 0; }