/*******************************************************
1 2 3 4 5 6 7 8 9
32 33 34 35 36 37 38 39 10
31 56 57 58 59 60 61 40 11
30 55 72 73 74 75 62 41 12
29 54 71 80 81 76 63 42 13
28 53 70 79 78 77 64 43 14
27 52 69 68 67 66 65 44 15
26 51 50 49 48 47 46 45 16
25 24 23 22 21 20 19 18 17
*******************************************************/
#include
#include
#define N 102
int main() {
int i, j, n, k;
int arr[N][N];
printf("矩阵大小:");
scanf("%d", &n);
n = n > 100 ? 100 : n;
memset(arr, 0, sizeof(arr));
for(i = 0; i <= n; ++i)
arr[0][i] = arr[n + 1][i] = arr[i][0] = arr[i][n + 1] = 1;
i = j = k = 1;
while(true) {
arr[i][j] = k++;
if(arr[i][j - 1] != 0 && arr[i + 1][j] == 0)
++i;
else if(arr[i][j + 1] == 0)
++j;
else if(arr[i - 1][j] == 0)
--i;
else if(arr[i][j - 1] == 0)
--j;
else
break;
}
for(j = 1; j <= n; ++j){
for(i = 1; i <= n; ++i)
printf("%2d ", arr[i][j]);
printf("\n");
}
return 0;
}
阅读(2481) | 评论(0) | 转发(0) |