#include #include #include #include #include #include using namespace std; int b[30][30]; int dx[4] = { 1, 0, -1, 0 }, dy[4] = { 0, 1, 0, -1 }; int main() { cin.tie(0); ios::sync_with_stdio(false); int N; cin >> N; int nx = 0, ny = 0, dir = 0; for(int i = 1; i <= N * N; i++) { b[ny][nx] = i; int nnx = nx + dx[dir], nny = ny + dy[dir]; if(nnx < 0 || N <= nnx || nny < 0 || N <= nny || b[nny][nnx] != 0) { dir = (dir + 1) % 4; } nx += dx[dir]; ny += dy[dir]; } for(int i = 0; i < N; i++) { for(int j = 0; j < N; j++) { cout << setw(3) << setfill('0') << b[i][j] << " "; } cout << endl; } }