Bootstrap

C语言数字递增程序,c语言:使一个N*N二维数组输出呈顺时针螺旋递增

#include

int main( ){

int i, j, a[100][100]={0}, n, k = 1;

printf("please input a number n:");

while(scanf("%d", &n)!=EOF){

k = 1;

for (i = 1; i <= n / 2; ++ i){

for (j = i; j <= n - i; ++ j) /* 左侧 */

a[i][j] = k ++;

for (j = i; j <= n - i; ++ j) /* 下方 */

a[j][n + 1 - i] = k ++;

for (j = n - i + 1; j >= i + 1; j = j - 1) /* 右侧 */

a[n + 1 - i][j] = k ++;

for (j = n - i + 1; j >= i + 1; j = j - 1) /* 上方 */

a[j][i] = k ++;

}

if(n != 1){

for (i = 1; i <= n; ++ i){

for (j = 1; j <= n; ++ j)

printf("%5d", a[i][j]); printf("\n");

}

}

else

printf("%5d\n",n);

printf("please input a number n:");

}

return 0;

}

执行效果如下:

0818b9ca8b590ca3270a3433284dd417.png

;