apl-mhd

coinChange 1 d array

Mar 27th, 2018
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.02 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <climits>
  4. #include <algorithm>
  5. #include <vector>
  6. #include <stack>
  7. #include <queue>
  8. #include <cmath>
  9. using namespace std;
  10.  
  11.  
  12. int main() {
  13.  
  14.  
  15.     int coins [] = {2,3,4,23};
  16.  
  17.  
  18.     int max =999;
  19.  
  20.  
  21.  
  22.     int M[8];
  23.     M[0]=0;
  24.  
  25.     for(int i=1; i<8; i++)
  26.         M[i] = 9999;
  27.  
  28.     int track[8];
  29.     track[0]=0;
  30.  
  31.  
  32.  
  33.  
  34.  
  35.     for(int i=0; i<4; i++){
  36.         for(int j=1; j<=7; j++){
  37.  
  38.             if(j>=coins[i]){
  39.  
  40.                 if(1+M[j-coins[i]]< M[j]){
  41.  
  42.                     M[j] = 1+M[j-coins[i]];
  43.  
  44.  
  45.                     track[j] = coins[i];
  46.                 }
  47.  
  48.                // M[j] = min(1+M[j-coins[i]],M[j]);
  49.  
  50.             }
  51.  
  52.  
  53.             }
  54.         }
  55.  
  56.  
  57.  
  58.     for(int i=1; i<8; i++)
  59.         cout<<M[i]<<" ";
  60.  
  61.     cout<<endl;
  62.  
  63.     for(int i=0; i<8; i++)
  64.     cout<<i<<" : "<<track[i]<<endl;
  65.  
  66.     cout<<endl;
  67.  
  68.     int end =7;
  69.  
  70.     while(end !=0){
  71.  
  72.         cout<<track[end]<<" ";
  73.  
  74.         end -=track[end];
  75.     }
  76.  
  77.  
  78.     return 0;
  79. }
Add Comment
Please, Sign In to add comment