#include <iostream>
using namespace std;
void printPascalTriangle(int n) {
int dp[n][n];
for (int i = 0; i < n; i++) {
dp[i][0] = 1;
dp[i][i] = 1;
}
for (int i = 2; i < n; i++) {
for (int j = 1; j < i; j++) {
dp[i][j] = dp[i-1][j-1] + dp[i-1][j];
}
}
for (int i = 0; i < n; i++) {
for (int j = 0; j <= i; j++) {
cout << dp[i][j] << " ";
}
cout << endl;
}
}
int main() {
int n;
cout << "Enter the number of rows for Pascal's Triangle: ";
cin >> n;
printPascalTriangle(n);
return 0;
}
这段代码实现了C++中杨辉三角形的迭代方法。用户可以输入想要显示的行数,然后程序会打印出相应的杨辉三角形。