pod*_*nro -1 c++ algorithm math binomial-coefficients
这是我的代码:
#include <iostream>
using namespace std;
int main()
{
int n,k,i,x;
cout << "Enter a row number for Pascal's Triangle: ";
cin >> n;
for(i=0;i<=n;i++)
{
x=1;
for(k=0;k<=i;k++)
{
cout << x << '\t';
x = x * (i - k) / (k + 1);
}
cout << endl;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
如何更改它以便它只显示第n行而不是整个三角形?TIA.
只需删除外循环:
int main()
{
cout << "Enter a row number for Pascal's Triangle: ";
cin >> n;
int x = 1;
for (int k = 0; k <= n; k++)
{
cout << x << '\t';
x = x * (n - k) / (k + 1);
}
cout << endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)