Advertisement
apl-mhd

coinChange

Oct 2nd, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <cmath>
  5.  
  6. using namespace std;
  7.  
  8. int main()
  9. {
  10.  
  11. int coin[9] = {1, 2, 5,10, 20, 50, 100, 500, 1000};
  12.  
  13. int tk, sum=0, i = 8;
  14.  
  15.  
  16. cin>>tk;
  17.  
  18. while( i != -1){
  19.  
  20. if(coin[i] <=tk){
  21.  
  22. sum = sum + coin[i];
  23. printf("%d + ", coin[i]);
  24. tk = tk - coin[i];
  25.  
  26.  
  27. }
  28.  
  29. else
  30.  
  31. i--;
  32. }
  33.  
  34. printf("= %d\n", sum);
  35.  
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement