提交时间:2019-12-19 20:50:45
运行 ID: 3511
#include<iostream> int main( ){ int i, j, a[100][100]={0}, n, k = 1; printf("please input a number n:"); while(scanf("%d", &n)!=EOF){ k = 1; for (i = 1; i <= n / 2; ++ i){ for (j = i; j <= n - i; ++ j) /* 左侧 */ a[i][j] = k ++; for (j = i; j <= n - i; ++ j) /* 下方 */ a[j][n + 1 - i] = k ++; for (j = n - i + 1; j >= i + 1; j = j - 1) /* 右侧 */ a[n + 1 - i][j] = k ++; for (j = n - i + 1; j >= i + 1; j = j - 1) /* 上方 */ a[j][i] = k ++; } if(n != 1){ for (i = 1; i <= n; ++ i){ for (j = 1; j <= n; ++ j) printf("%5d", a[i][j]); printf("\n"); } } return 0; }