1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58


/**
* C (m, n): select m from n elements, 0 <= m <= n
* C(m, n) = n! / (m! * (n-m)!)
* C(0, n) = 1
* C(m+1, n) = C(m, n) * ((n-m) / (m+1))
*/
#include <vector> #include <iostream> using namespace std; /* C (m, n): select m from n elements, 0 <= m <= n
* C(m, n) = n! / (m! * (n-m)!)
* C
*/
vector<int> getRow(int rowIndex) { vector<int> result(rowIndex+1, 1); //calcuate half, and mirrow to whole array int size = rowIndex/2+1; for (int i = 1; i < size; ++i) { int value = 0; // To prevent from integer overflow if (rowIndex >= 30) { value = int(((long long)result[i-1]) * (rowIndex-i+1) / i); } else { // but also reduce type casting if not neccessary value = result[i-1] * (rowIndex-i+1) / i; } result[rowIndex-i] = result[i] = value; } return result; } void printV(vector<int> v) { size_t si = v.size(); for (int i = 0; i < si; ++i) { cout << v[i] << ", "; } cout << endl; } int main (int argc, const char * argv[]) { cout << "Pascal's Triangle of 7:" << endl; printV(getRow(7)); cout << endl << "Pascal's Triangle of 21:" << endl; printV(getRow(21)); return 0; }
View Program Text


Test Status