fork(2) download
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4.  
  5. int count( int S[], int m, int n )
  6. {
  7. // table[i] will be storing the number of solutions for
  8. // value i. We need n+1 rows as the table is consturcted
  9. // in bottom up manner using the base case (n = 0)
  10. int table[n+1];
  11.  
  12. // Initialize all table values as 0
  13. memset(table, 0, sizeof(table));
  14.  
  15. // Base case (If given value is 0)
  16. table[0] = 1;
  17.  
  18. // Pick all coins one by one and update the table[] values
  19. // after the index greater than or equal to the value of the
  20. // picked coin
  21. for(int i=0; i<m; i++)
  22. for(int j=n; j>=S[i]; j--) // One line change here.
  23. table[j] += table[j-S[i]];
  24.  
  25. return table[n];
  26. }
  27.  
  28. int main() {
  29. int coins[] = {2,5,3, 6};
  30. cout << count(coins, 4, 10);
  31. return 0;
  32. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
1