/* BC133 回型矩阵 */ #include <stdio.h> int main() { int n = 0; int array[20][20] = { 0 }; scanf("%d", &n); int num = 1; int lmargin = 0; int rmargin = n - 1; int count = 1; int row = 0, col = 0; while (lmargin <= rmargin) { while (col <= rmargin) //左上角->右上角 array[row][col++] = num++; count++, col--, row++; while (row <= rmargin) //右上角->右下角 array[row++][col] = num++; count++, row--, col--; while (col >= lmargin) //右下角->左下角 array[row][col--] = num++; count++, col++, row--; if (count == 4) //改变边界 lmargin++, rmargin--, count = 0; while (row >= lmargin) array[row--][col] = num++; //左下角->左上角 count++, row++, col++; } for (row = 0; row < n; row++) { for (col = 0; col < n; col++) { printf("%d ", array[row][col]); } printf("\n"); } return 0; }