c++楊輝三角代碼怎么寫

c++
小億
90
2024-04-26 16:39:44

#include using namespace std;

int main() { int numRows;

cout << "Enter the number of rows for Pascal's Triangle: ";
cin >> numRows;

int triangle[numRows][numRows];

for (int i = 0; i < numRows; i++) {
    triangle[i][0] = 1;
    triangle[i][i] = 1;
}

for (int i = 2; i < numRows; i++) {
    for (int j = 1; j < i; j++) {
        triangle[i][j] = triangle[i-1][j-1] + triangle[i-1][j];
    }
}

cout << "Pascal's Triangle:" << endl;
for (int i = 0; i < numRows; i++) {
    for (int j = 0; j <= i; j++) {
        cout << triangle[i][j] << " ";
    }
    cout << endl;
}

return 0;

}

0