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