P5732 【深基5.习7】杨辉三角 - 洛谷
![![[Pasted image 20250305184653.png]]](https://i-blog.csdnimg.cn/direct/d86371deb4e84f7b8a3c8a737953146c.png)
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> a(n, vector<int> (n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0)
{
a[i][j] = 1;
}
if (j == i)
{
a[i][j] = 1;
}
if (i >= 2 && j >= 1)
a[i][j] = a[i-1][j-1] + a[i-1][j];
}
}
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
cout << a[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
合并成一次遍历
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<vector<int>> a(n, vector<int> (n));
for (int i = 0; i < n; i++)
{
for (int j = 0; j <= i; j++)
{
if (j == 0)
{
a[i][j] = 1;
}
if (j == i)
{
a[i][j] = 1;
}
if (i >= 2 && j >= 1)
a[i][j] = a[i-1][j-1] + a[i-1][j];
cout << a[i][j] << ' ';
}
cout << '\n';
}
return 0;
}
B2099 矩阵交换行 - 洛谷
#include <bits/stdc++.h>
using namespace std;
int main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
vector<vector<int>> a(5, vector<int>(5));
for (int i = 0; i < 5; i++)
{
for (int j = 0; j < 5; j++)
{

最低0.47元/天 解锁文章
679

被折叠的 条评论
为什么被折叠?



