题解 | #回型矩阵#
回型矩阵
https://www.nowcoder.com/practice/36d5dfddc22c4f5b88a5b2a9de7db343
#include <stdio.h> void hui(int arr[20][20],int n) { int i = 0; int j = 0; int k = n / 2;//圈数 int m = 1;//输入 int r = 0;//行 int c = 0;//列 while (k--) { for (j = c; j < n-c; j++) { arr[i][j] = m; m++; } j = n - c - 1; for (i = r + 1; i < n-r; i++) { arr[i][j] = m; m++; } i = n - r - 1; for (j = j - 1; j >=c; j--) { arr[i][j] = m; m++; } j = c; c++; for (i = i - 1; i >=r+1; i--) { arr[i][j] = m; m++; } r++; i = r; } if (n % 2 != 0)//奇数中间再补一个 { arr[n / 2][n / 2] = m; } } int main() { int n = 0; scanf("%d", &n); int arr[20][20]; hui(arr, n); int i = 0; for (i = 0; i < n; i++) { int j = 0; for (j = 0; j < n; j++) { printf("%d ", arr[i][j]); } printf("\n"); } return 0; }