我需要一些在 c++ 中打印 Pascal 三角形的程序的帮助。我需要间距看起来像这样:
How many rows: 4
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
但它看起来像这样:
Enter a number of rows: 4
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
我的代码是:
#include <iostream>
#include <iomanip>
using namespace std;
int combinations (int n, int k) {
if (k == 0 || k == n) {
return 1;
}
else {
return combinations(n-1,k-1) + combinations(n-1,k);
}
}
int main ( ) {
int rows;
cout << "Enter a number of rows: ";
cin >> rows;
for(int r = 0; r < rows+1; r++) {
cout << " " << "1";
for(int c = 1; c < r+1; c++) {
cout << " " << combinations(r, c) << ' ';
}
cout << endl;
}
}
有人可以帮我确定间距吗?