Posted By

ldominov on 01/16/13


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

iamd3n


binomni


 / Published in: C++
 

implementation of binom coefficient dynamic programming c++

  1. #include <iostream>
  2.  
  3. using namespace std;
  4. int min(int i,int k){
  5. if (i<k) return i;
  6. return k;
  7. }
  8. int binomni(int n, int k){
  9. int C[n+1][k+1];
  10. //C[n][0]=C[n][n]=1;
  11. for(int i=0; i<=n; i++)
  12. for (int j=0;j<=min(i,k);j++){
  13. if(j==0 || i==j)
  14. C[i][j]=1;
  15. else
  16. C[i][j]=C[i-1][j-1] + C[i-1][j];
  17. }
  18. return C[n][k];
  19. }
  20.  
  21. int main(){
  22. int n,k;
  23. cout << "Unesite n: ";
  24. cin >> n;
  25. cout << "Unesite k: ";
  26. cin >> k;
  27. cout << "\nBinomni koeficijent iznosi: " << binomni(n,k) << endl;
  28. cin >> n;
  29. return 0;
  30. }

Report this snippet  

You need to login to post a comment.