#include
using namespace std;
int YH(int x,int y);
int main()
{
int i,j,n;
cout<<"请输入需要的行数:\n";
cin>>n;
for (i=1;i<=n;i++)
{
for (j=0;j<=n-i;j++)
cout<<" ";
for (j=1;j<=i;j++)
cout<<" "<
return 0;
}
int YH(int x,int y)
{
int z;
if (y==1||y==x)
z=1;
else
z=YH(x-1,y-1)+YH(x-1,y);
return z;
}
拿去用,可以输出任意行,只要改变参数即可。
#include
using namespace std ;
// Output pascal triangle
// n: number of lines to print
void PascalTriangle(int n)
{
// Allocate memory to store numbers
int* a = new int[n] ;
for (int i = 0; i < n; ++i)
{
a[i] = 0 ;
}
a[0] = 1 ;
for (int i = 0; i < n; ++i)
{
// Compute current line
// You must compute from back to front, or you will overwrite the existing values
for (int j = i; j > 0; --j)
{
a[j] += a[j - 1];
}
// Print space
for (int j = 0; j < n - i; ++j)
{
cout << " " ;
}
// Print number
for (int j = 0; j < i + 1; ++j)
{
cout << a[j] << " " ;
}
cout << endl ;
}
}
int main(void)
{
PascalTriangle(5) ;
system("pause") ;
return 0 ;
}