题解 | #蛇形矩阵#
蛇形矩阵
https://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <stdio.h> const int M = 101; int main() { int array[M] ={0}; int N1,N2,out; //N1表示行数,N2表示列数,out表示输出 int base1=1,base2 = 0;//每一轮的基底数,逻辑上行优先,1为行基底,2为列基底 int swap = 0;//因为在列循环中输出,所以需要swap用于暂存base1 scanf("%d",&N1); N2 = N1; for(int i =1; i<=N1;++i,--N2){ swap = base1; printf("%d ",base1); for(int j = 1;j<N2;++j){ base2 = swap+j+i; printf("%d ",base2); swap = base2; if(j == N2-1){ printf("\n"); base2 = 0; } } base1 += i; } return 0; }