楊輝三角,又稱賈憲三角形,帕斯卡三角形,是二項式系數在三角形中的壹種幾何排列。在歐洲,這個表叫做帕斯卡三角形。
這是楊輝三角:
代碼如下:
#include <stdio.h>
#include <stdlib.h>
const int length = 10;? // 定義楊輝三角的大小
int main(void)
{
int nums[length][length]; ?
int i, j; ?
/*計算楊輝三角*/
?
for(i=0; i<length; i++)
{
nums[i][0] = 1;
nums[i][i] = 1;?
for(j=1; j<i; j++)
nums[i][j] = nums[i-1][j-1] + nums[i-1][j];
?
} ?
/*打印輸出*/
?
for(i=0; i<length; i++)
{?
for(j=0; j<length-i-1; j++)
printf(" ? ");
for(j=0; j<=i; j++)
printf("%-5d ", nums[i][j]); ?
putchar('\n');
?
}
?
getchar();// 暫停?
return EXIT_SUCCESS;?
}