-
試著編程完成打印楊輝三角
代碼實(shí)現(xiàn)如下
#include <stdio.h>
#define LINE 16
void main()
{
int a[LINE][LINE] = { 0 }, i, j, k; /*初始化數(shù)組*/
for (i = 0; i < LINE; i++)
a[i][0] = 1; /*填充數(shù)組第一列數(shù)字為 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]; /*楊輝算法賦值*/
/*賦值完畢現(xiàn)在進(jìn)行打印輸出*/
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");
}
}
- 完