-
试着编程完成打印杨辉三角
代码实现如下
#include <stdio.h>
#define LINE 16
void main()
{
int a[LINE][LINE] = { 0 }, i, j, k; /*初始化数组*/
for (i = 0; i < LINE; i++)
a[i][0] = 1; /*填充数组第一列数字为 1*/
for (i = 1; i < LINE; i++)
for (j = 1; j < LINE; j++)
a[i][j] = a[i - 1][j - 1] + a[i - 1][j]; /*杨辉算法赋值*/
/*赋值完毕现在进行打印输出*/
for (i = 0; i < LINE; i++)
{
for (k = i; k < LINE - 1; k++)
printf(" "); /*打印格式里的空格*/
for (j = 0; j < LINE; j++)
{
if (a[i][j] != 0)
printf("%-6d", a[i][j]);
}
printf("\n");
}
}
- 完