SabirSazzad

greedy knapsack Coin

Oct 31st, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.15 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct Coin{
  4.     float value;
  5.     int count;
  6. };
  7. void greedyKnapsackMoney(Coin c[], int n, float amount)
  8. {
  9.     int i;
  10.     float currentAmount=0;
  11.  
  12.     for(i=0;i<n;i++)
  13.     {
  14.         if(currentAmount+c[i].value<= amount)
  15.         {
  16.             currentAmount = currentAmount+c[i].value;
  17.             c[i].count++;
  18.             i--;
  19.         }
  20.     }
  21.     cout << "\n\n----------------------"<<endl;
  22.     for(i=0; i<n; i++)
  23.     {
  24.         if(c[i].count != 0)
  25.         {
  26.             if(c[i].value >= 1)
  27.             {
  28.                 cout << " " << c[i].value << " Taka Coin Quantity--> " << c[i].count <<endl;
  29.             }
  30.             else
  31.             {
  32.                 cout << " " << c[i].value << " Poisa Coin Quantity--> " << c[i].count <<endl;
  33.             }
  34.         }
  35.     }
  36.  
  37. }
  38.  
  39. int main()
  40. {
  41.     float amount;
  42.     cout << "Input Amount: ";
  43.     cin >> amount;
  44.     float coinValue[8] = {5,2,1,0.50,0.25,0.10,0.05,0.01};
  45.     Coin c[8];
  46.     for(int i=0; i<8; i++)
  47.     {
  48.         c[i].value = coinValue[i];
  49.         c[i].count = 0;
  50.     }
  51.     greedyKnapsackMoney(c,8,amount);
  52.  
  53.     return 0;
  54. }
Advertisement
Add Comment
Please, Sign In to add comment