提交时间:2026-06-15 19:35:09
运行 ID: 91748
#include <bits/stdc++.h> using namespace std; int dx[] = {0 , -1 , 0 , 1} ; int dy[] = {1 , 0 , -1 , 0} ; int main() { int n; while(cin >> n) { int a[11][11] , pos = 1 ; memset(a , 0 , sizeof a) ; int x = 1 , y = 1 ; for(int i = 1 ; i <= n * n ; i++ ) { a[x][y] = i ; if(x + dx[pos] > n || x + dx[pos] < 1 || y + dy[pos] > n || y + dy[pos] < 1) pos = (pos + 1) % 4 ; x += dx[pos] , y += dy[pos] ; } for(int i = 1 ; i <= n ; i++) { for(int j = 1 ; j <= n ; j++) cout << a[i][j] << ' ' ; cout << endl ; } } return 0; }