题解 | #蛇形矩阵#
蛇形矩阵
http://www.nowcoder.com/practice/649b210ef44446e3b1cd1be6fa4cab5e
#include <stdio.h>
int main()
{
int array[100] = {0};
int num = 0;
scanf("%d", &num);
int count = 1;
/* 给首行赋值 */
array[0] = 1;
for(int i = 1; i <= num; i++)
{
array[i] = i+1+array[i-1];
//设num = 4;
//array[0] = 1; array[1] = 3; array[2] = 6; array[3] = 10;
}
/* 打印首行 */
for(int i = 0; i < num; i++)
{
printf("%d ", array[i]);
}
printf("\n");
/* 打印剩余行,规则:第二行为上一行元素减一,再去掉一个数 */
for(int i = num-1; i > 0; i--)
{
for(int j = 0; j < i; j++)
{
printf("%d ", array[j+1]-1);
array[j] = array[j+1]-1; //将下一行数据写入数组中
}
printf("\n");
}
return 0;
}