1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
| #include <stdio.h>
int main(){ int n, i, j; int a[20][20]; int data = 1; //数组数据
scanf("%d",&n);
int up = 0, down = n-1, left = 0, right = n-1;
//回型递增定义数组数据 while (data <= n*n) { for (i = left; i <= right; i++) { a[up][i] = data++; } up++;
for (i = up; i <= down; i++) { a[i][right] = data++; } right--;
for (i = right; i >= left; i--) { a[down][i] = data++; } down--;
for (i = down; i >= up; i--) { a[i][left] = data++; } left++; }
//输出 for (i = 0; i < n; i++) { for (j = 0; j < n; j++) { printf("%4d",a[i][j]); } printf("\n"); }
return 0; }
|