#include <iostream> #include <vector> using namespace std; void generate_yanghui_triangle(int n) { vector<vector<int>> triangle; for (int i = 0; i < n; i++) { if (i == 0) { vector<int> row = {1}; triangle.push_back(row); } else if (i == 1) { vector<int> row = {1, 1}; triangle.push_back(row); } else { vector<int> row = {1}; for (int j = 1; j < i; j++) { row.push_back(triangle[i-1][j-1] + triangle[i-1][j]); } row.push_back(1); triangle.push_back(row); } } for (const auto& row : triangle) { for (int num : row) { cout << num << " "; } cout << endl; } } int main() { int n; cin >> n; generate_yanghui_triangle(n); return 0; }
评判结果