题解 | #回型矩阵#
回型矩阵
http://www.nowcoder.com/practice/36d5dfddc22c4f5b88a5b2a9de7db343
int main() {
int n;
scanf("%d", &n);
int val[n][n], count = 1;
int x = 0, y = n - 1;
while (count != n * n + 1) {
for (int i = x; i <= y; i++) //上行循环
val[x][i] = count++;
for (int i = x + 1; i <= y; i++) //后列循环
val[i][y] = count++;
for (int i = y - 1; i >= x; i--) //下行循环
val[y][i] = count++;
for (int i = y - 1; i >= x + 1; i--) //前列循环
val[i][x] = count++;
x++; //行加1
y--; //列减1
}
for (int i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
printf("%d ", val[i][j]);
}
printf("\n");
}
return 0;
}