用C编程实现打印杨辉三角
程序员文章站
2022-05-12 19:12:38
#include
#include
#define line 9
//第n行的m个数可表示为 C(n-1,m...
#include<stdio.h> #include<stdlib.h> #define line 9 //第n行的m个数可表示为 C(n-1,m-1),即为从n-1个不同元素中取m-1个元素的组合数。 //即为:num!/(count!*(num-count)!) "!"为阶乘 int factorial(int z) //阶乘 { int i,count_sum=1; for (i = 1; i <= z; i++) { count_sum *= i; } return count_sum; } int main() { int num, count, kong; //行号-num 数字-count 空格-kong 打印序号-xuhao for (num = 0; num < line; num++) { for (kong = line-num; kong > 0; kong--) printf(" "); for (count = 0; count <= num; count++) printf("%2d ", factorial(num)/ (factorial(count)*factorial(num-count))); printf("\n"); } system("pause"); return 0; }
下一篇: Hadoop介绍及伪分布式部署