Kamrul13981

coin change and bin pack

Aug 8th, 2021
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.27 KB | None | 0 0
  1. 1.//coinchange
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. int notes[] = {1, 10, 5, 2, 20, 100, 500, 50,1000};
  7. int s = sizeof(notes)/sizeof(notes[0]);
  8.  
  9. void findMin(int value)
  10. {
  11.     sort(notes, notes + s);//1,2,5,10,20,50,100,500,1000
  12.  
  13.     vector <int> ans;
  14.  
  15.     for (int i = s-1; i >= 0; i--)
  16.     {
  17.         while (value >= notes[i])
  18.         {
  19.             value =value- notes[i];
  20.             ans.push_back(notes[i]);
  21.         }
  22.     }
  23.  
  24.     for (int i = 0; i < ans.size(); i++)
  25.         cout << ans[i] << " ";
  26. }
  27.  
  28. int main()
  29. {
  30.     int n;
  31.     cout << "Your friend's amount: ";
  32.     cin >> n;
  33.  
  34.     cout << "You will give him: ";
  35.     findMin(n);
  36.  
  37.     return 0;
  38. }
  39.  
  40. 2.//Bin packing
  41. #include <bits/stdc++.h>
  42.  
  43. using namespace std;
  44.  
  45. int nextFit(int weight[], int n, int c)
  46. {
  47.     int result = 0, bin_rem = c;
  48.  
  49.     for (int i = 0; i < n; i++) {
  50.         if (weight[i] > bin_rem) {
  51.             result++;
  52.             bin_rem = c - weight[i];
  53.         }
  54.         else
  55.             bin_rem = bin_rem -weight[i];
  56.     }
  57.  
  58.     return result;
  59. }
  60.  
  61. int main()
  62. {
  63.     int weight[] = {2, 5, 4, 7, 1, 3, 8};
  64.     int c = 10;
  65.     int n = sizeof(weight)/sizeof(weight[0]);
  66.  
  67.     cout << "Number of bins required: "
  68.          << nextFit(weight, n, c);
  69.  
  70.     return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment