#include "stdafx.h"
#include "iostream"
using namespace std;
//杨辉三角
int yh(int i, int j) {
if (j == i || j == 1) {
return 1; //每一行的第一个和最后一个为1
}
else {
return yh(i - 1, j - 1) + yh(i - 1, j); //第i行第j列 等于 第i-行的第j-1个和第j个相加
}
}
int main()
{
int n;
cout << "输入行数";
cin >> n;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= i; j++) {
cout << yh(i, j) << " ";
}
cout << endl;
}
return 0;
}
运行结果