我需要一些幫助,用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