#include <stdio.h>
long factorial(int num)
{
int counter;
int fact = 1;
for (counter = num; counter > 0; counter--) fact *= counter;
return fact;
}
float combinations(int n, int k)
{
int numerator = factorial(n);
int denominator = factorial(k) * factorial(n-k);
float fraction = numerator/denominator;
return fraction;
}
int main()
{
printf("How many rows of Pascal\'s triangle should I print?\t");
int rows = GetInteger();
int counter;
int counter2;
for (counter = 1; counter <= rows; counter++)
{
int y = rows-counter;
for (; y > 0; y--) printf(" ");
for (counter2 = 0; counter2 <= counter; counter2++)
printf("%6.0lu", (long) combinations(counter, counter2));
printf("\n");
}
}
每次我经过十二排,数字开始减少。我究竟做错了什么?
而且,GetInteger()
只是scanf()
一些修饰。我 100% 确定它可以完美运行。