fork download
  1. /*
  2.  * Description: Finding Comb(n,k)
  3.  * Dynamic Programming Technique
  4.  *
  5.  * DP Based on recursive formula.
  6.  * C(n,k) = 1 if k == 0 or n == k
  7.  * C(n,k) = C(n-1,k-1) + C(n-1,k)
  8.  *
  9.  */
  10.  
  11. #include <bits/stdc++.h>
  12. #define LLI long long int
  13. #define SIZE 101
  14.  
  15. using namespace std;
  16.  
  17. LLI C[SIZE][SIZE], i, j, n, k;
  18.  
  19. int main(int argc, char const *argv[]) {
  20.  
  21. n = 6;
  22. k = 4;
  23.  
  24. C[ 0 ][ 0 ] = 1;
  25.  
  26. for(i = 1; i <= n; ++i) {
  27.  
  28. for(j = 0; j <= i; ++j) {
  29.  
  30. if(0 == j || i == j) C[i][j] = 1;
  31.  
  32. else
  33.  
  34. C[i][j] = C[i-1][j-1] + C[i-1][j];
  35.  
  36. }
  37. }
  38.  
  39. for(i = 0; i <= n; ++i) {
  40.  
  41. for(j = 0; j <= i; ++j) {
  42.  
  43. cout<<C[i][j]<<" ";
  44. }
  45. cout<<endl;
  46. }
  47.  
  48. cout<<C[n][k];
  49.  
  50. return 0;
  51. }
  52.  
Success #stdin #stdout 0.01s 5548KB
stdin
Standard input is empty
stdout
1 
1 1 
1 2 1 
1 3 3 1 
1 4 6 4 1 
1 5 10 10 5 1 
1 6 15 20 15 6 1 
15