#include <stdio.h> #include <string.h> int map[100][100]; int main() { memset(map, 0, sizeof(map)); int n, i, j,count; int x = 1, y = 0; int tot = 1; scanf("%d", &n); count = (1 + n) * n / 2; //printf("%d\n\n",count); while(tot != count+1) { while(y < n && !map[x][y+1])//横行 { map[x][++y] = tot++; } if(tot == count+1) break; while(x < n && !map[x+1][y-1])//斜行 { map[++x][--y] = tot++; } if(tot == count+1) break; while(y > 0 && !map[x-1][y])//竖行 { map[--x][y] = tot++; } if(tot == count+1) break; } for(i = 1; i <= n; i++) { for(j = 1; j <= n; j++) { if(map[i][j] != 0) { printf("%-4d",map[i][j]); } } printf("\n"); } return 0; }