01/10/2018, 00:58
Ai giải thích giúp em với
#include <iostream>
#include <vector>
using namespace std;
void print_pascals_triangle(int rows);
void print_row(vector<int> row_values);
/* main()
Prints Pascal's triangle
*/
int main() {
int rows;
cout << "Pascal's triangle!" << endl;
do {
cout << "Enter how many rows to print (at least 0): ";
cin >> rows;
} while(rows < 0);
print_pascals_triangle(rows);
return 0;
}
/* print_pascals_triangl(rows)
Prints the first rows of Pascal's triangle, left-aligned.
rows: number of rows to print
*/
void print_pascals_triangle(int rows) {
vector<int> row_values;
for(int row = 0; row < rows; ++row) {
// Update for the next row
vector<int> new_row = row_values;
new_row.push_back(1); // Last column is always 1
// Compute the new row from the current row
for(int c = 1; c < new_row.size()-1; ++c)
new_row.at(c) = row_values.at(c-1) + row_values.at(c);
// Copy updated row back into the original
row_values = new_row;
// Print the updated row
print_row(row_values);
}
}
/* print_row(row_values)
Prints a vector<int> as a row of the triangle.
row_values: vector<int> of the values to print
*/
void print_row(vector<int> row_values) {
for(int value : row_values) {
cout << value << " ";
}
cout << endl;
}
Bài liên quan
Bạn chép code rồi nhờ giải thích???
http://staffwww.fullcoll.edu/aclifton/courses/cs123_fa16/lecture-19-projects-and-files.html
Ít nhất chép copy sang thì phải xóa comment, thay đổi tên biến, hàm, … này nọ để người khác không nhận ra chứ
This topic was automatically closed 8 hours after the last reply. New replies are no longer allowed.