SabirSazzad

greedy knapsack Money

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