思路感觉还是比较好想的。
从 1 到 n 依次算。先往右,走到头往下,再走到头往左,以此类推。
#include<stdio.h>int main()
{int n, i, j, k=1,t=0;scanf("%d", &n);int a[100][100];if (n % 2 == 0)t = n / 2;elset = n / 2 + 1;for ( i = 0; i < n; i++){for (j = i; j < n-i; j++)//先往右a[i][j] = k++;for (j =i+1; j <n-i; j++)//走到头往下a[j][n-i-1] = k++;for (j = n - i - 2; j >=i; j--)//再走到头往左a[n - i - 1][j] = k++;for (j = n - i - 2; j >i; j--)//往上a[j][i] = k++;}for ( i = 0; i < n; i++){for (j = 0; j < n; j++)printf("%3d", a[i][j]);printf("\n");}return 0;
}