简单做法

#include<iostream>
using namespace std;
#include<string>
#include<memory>
void kongge(int n)//打印空格
{
for (int i = 0; i < n; i++)
{
cout << " "<<" ";
}
}
int main()
{
int arr[50][50];
for (int i = 0; i < 50; i++)
{
for (int k = 0; k < 50; k++)
{
arr[i][k] = 0;
}
}
arr[1][1] = 1;
arr[1][2] = 1;
for (int i = 1; i < 50; i++)
{
arr[i][1] = 1;
}
int n;
cout << "请输入行数:";
cin >> n;
kongge(n);
cout << "1" << endl;
kongge(n - 1);
cout << "1" <<" "<<" "<<" " << "1" << endl;
for (int i = 2; i <= n; i++)
{
kongge(n-i);
cout << arr[i][1]<<' '<<" "<<" ";
for (int k = 2; k <= i+1; k++)
{
arr[i][k] = arr[i - 1][k - 1] + arr[i - 1][k];//实现杨辉三角的算法
if (arr[i][k] == 0)
{
break;
}
cout << arr[i][k] << ' '<<" "<<" ";
}
cout << endl;
}

return 0;
}


效果

杨辉三角形(开方做法本源)_i++