题解 | #回型矩阵#
回型矩阵
https://ac.nowcoder.com/acm/problem/22230
#include<stdio.h> int main(){ int n; scanf("%d",&n); int a[n][n]; int left=0,right=n-1,top=0,bottom=n-1; int value=1; while(left<=right&&top<=bottom){ //填充上面,从左到右 for(int i=left;i<=right;i++){ a[top][i]=value++; } top++;//上边界向下收缩 for(int i=top;i<=bottom;i++){ a[i][right]=value++; } right--;//右边界向左收缩 for(int i=right;i>=left;i--){ a[bottom][i]=value++; } bottom--;//下边界向上收缩 for(int i=bottom;i>=top;i--){ a[i][left]=value++; } left++;//左边界向右收缩 } for(int i = 0; i < n; i++){ for(int j = 0; j < n; j++){ printf("%d ",a[i][j]); } printf("\n"); } }