我目前正在处理的帕斯卡三角函数有问题。我的主要问题是它的格式不像半金字塔,而是几乎像曲线。到目前为止,这是我的代码,我将在我设置 numRows=6 的末尾包含当前输出:
#include <iostream>
#include <math.h>
using namespace std;
double factorial(double x)
{
int i;
double x_new=1;
for (i=x; i>=1; i--)
{
x_new*=i;
}
return x_new;
}
double Pascal(double n, double p){
double d=n-p;
double fact_1;
double fact_2;
double fact_3;
fact_1=factorial(n);
fact_2=factorial(p);
fact_3=factorial(d);
return fact_1/(fact_2*fact_3);
}
int main()
{
int n;
double numRows;
double numCollumns;
cout << "Enter number of rows: ";
cin >> numRows;
n=numRows+1;
for (numRows=0; numRows<n;numRows++)
{
for(numCollumns=0; numCollumns<=numRows;numCollumns++)
{
cout << Pascal(numRows, numCollumns) << " ";
}
cout << endl;
}
}
//this is the output of the function when numRows is set to 6:
// 1
// 1 1
// 1 2 1
// 1 3 3 1
// 1 4 6 4 1
// 1 5 10 10 5 1