我需要一些帮助,用C++打印Pascal的三角形程序。我需要的间距看起来像这样:帕斯卡的三角形程序间距C++
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;
}
}
有人可以帮我把间隔的权利?
您提出问题的方式使您看起来像没有尝试解决特定问题。 – 2015-01-21 00:33:04
我尝试了很多事情,但我无法弄清楚如何让它工作。例如,我尝试了<< setw的各种组合。 – Kelton2 2015-01-21 00:34:10
我在过去为另一篇文章写了一个解决方案。你可以查看http://stackoverflow.com/questions/19898756/pascals-triangle-using-mainly-functions-in-c/ – Ares 2015-02-13 03:55:23