#include <iostream>
using namespace std;
int YH_triangle(int line, int row) //define a function that used to calculate Yang Hui's number
{
if (row == 1 || line == row)
{
return 1;
}else
{
return YH_triangle(line - 1, row - 1) + YH_triangle(line - 1, row);
}
}
int main()
{
int line;
cout << "input the number of lines you want:";
cin >> line;
cout << endl;
if (line == 0)
{
cout << "NONE";
return 0;
}
for (int i = 0; i < line; i++)
{
for (int j = 0; j <= i; j++)
{
cout << YH_triangle(i + 1, j + 1) << ' ';
}
cout << endl;
}
return 0;
}
然而,递归函数实在是太慢了!!!