Advertisement
apl-mhd

Zitu Sir Coin Change

Mar 27th, 2018
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.89 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. using namespace std;
  4. int M[8];
  5. int S[8];
  6. int makeChane(int c[], int n){
  7.  
  8.    if(M[n] == -1){
  9.  
  10.         int minval=9999, coinval =-1;
  11.  
  12.         for(int i=0; i<4; i++){
  13.  
  14.             if(c[i]<=n){
  15.  
  16.                 int tval = 1+makeChane(c, n-c[i]);
  17.  
  18.                     if(tval<minval) {
  19.                         minval = tval;
  20.                         coinval = c[i];
  21.                     }
  22.             }
  23.         }
  24.  
  25.         M[n] = minval;
  26.         S[n] = coinval;
  27.     }
  28.  
  29.     return M[n];
  30. }
  31.  
  32. int main() {
  33.  
  34.     M[0]=0;
  35.  
  36.     for(int i=1; i<8; i++)
  37.         M[i]=-1;
  38.     int coins[]= {2,3,4,90};
  39.     int n=7;
  40.     cout<<"Total "<<makeChane(coins, n)<<" coins need to make "<<n<<endl;
  41.  
  42.     int end = 7;
  43.     cout<<"coins are:"<<endl;
  44.     while(end !=0 ){
  45.  
  46.         cout<<S[end]<<" ";
  47.         end -=S[end];
  48.     }
  49.     cout<<endl;
  50.     return 0;
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement