打印三到十行的杨辉三角
#include <iostream>
using namespace std;
void triangle(int n);
int main()
{
int num;
do{
cout << "请输入行数: ";
cin >> num;
}while(num < 3 || num > 10);
triangle(num);
return 0;
}
void triangle(int n)
{
int a[10][10];
a[0][0] = 1;
a[1][0] = 1;
a[1][1] = 1;
cout << a[0][0] << endl << endl << a[1][0] << "\t" << a[1][1] << endl << endl;
for(int i = 2; i < n; i++)
{
for(int j = 0; j <= i; j++)
{
if(j == 0 || j == i)
{
a[i][j] = 1;
}
else
{
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
cout << a[i][j] << "\t";
}
cout << endl << endl;
}
}