楊輝三角(pascal's triangle)
阿新 • • 發佈:2019-01-26
輸入行數,輸出對應的楊輝三角
本題所用:
C(n,0)=1
C(n,k)=C(n,k-1)*(n-k+1)/k
執行結果如下:
//輸入行數,輸出對應的楊輝三角
#include <iostream>
#include <cstdlib>
#include <vector>
using namespace std;
int main() {
int n;
std::cin >> n;
for (int i = 0; i < n; i++) {
vector<int> v(i + 1 );
v[0] = 1;
cout << v[0] << " ";
for (int j = 1; j <= i; j++) {
v[j] = v[j - 1] * (i - j + 1) / j;
cout << v[j]<<" ";
}
cout << endl;
}
return EXIT_FAILURE;
}
附常用排列組合公式:
二項式定理(binomial theorem)