#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int yhsj[100][100];
int hang;
cin >> hang;
yhsj[0][0] = 1;
yhsj[1][0] = 1;
yhsj[1][1] = 1;
for (int i = 2; i < hang; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0)
{
yhsj[i][j] = 1;
}
if (j == i)
{
yhsj[i][j] = 1;
}
if(j!=0 and j!=i)
{
yhsj[i][j] = yhsj[i - 1][j] + yhsj[i - 1][j - 1];
}
}
}
for (int i = 0; i < hang; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == i)
{
cout << yhsj[i][j];
}
else
{
cout << yhsj[i][j] << " ";
}
}
cout << endl;
}
}
C++输出n行杨辉三角(初学)
最新推荐文章于 2024-01-06 20:32:36 发布