當前位置:編程學習大全網 - 編程軟體 - C語言中怎麽寫楊輝三角啊?

C語言中怎麽寫楊輝三角啊?

楊輝三角,又稱賈憲三角形,帕斯卡三角形,是二項式系數在三角形中的壹種幾何排列。在歐洲,這個表叫做帕斯卡三角形。

這是楊輝三角:

代碼如下:

#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;?

}

  • 上一篇:2015年臺式機配置 推薦 9000元左右 不包括顯示屏
  • 下一篇:mfc怎麽在桌面畫圖?
  • copyright 2024編程學習大全網