{ int a[100][100]; int n; cin >> n; int b = 1; int k; int i, j; for (k = 0; k < n / 2; k++) { for (i = k; i <= n - 1 - k; i++) a[k][i] = b++; for (j = k + 1; j < n - 1 - k; j++) a[j][n - 1 - k] = b++; for (i = n - 1 - k; i > k; i–) a[n - 1 - k][i] = b++; for (j = n - 1 - k; j > k; j–) a[j][k] = b++; } if (n % 2 == 1)a[(n - 1) / 2][(n - 1) / 2] = n * n; for (i = 0; i < n; i++) { for (j = 0; j < n; j++) printf("%4d", a[i][j]); cout << endl; }}分上右下左四块区域进行实现赋值,上两张图图一表示如何较彻底地理解一个全新的算法图二表示螺旋算法效果
作者:LOVEの心就是我
https://www.bilibili.com/read/cv1853272
出处: bilibili