Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 1.//coinchange
- #include <bits/stdc++.h>
- using namespace std;
- int notes[] = {1, 10, 5, 2, 20, 100, 500, 50,1000};
- int s = sizeof(notes)/sizeof(notes[0]);
- void findMin(int value)
- {
- sort(notes, notes + s);//1,2,5,10,20,50,100,500,1000
- vector <int> ans;
- for (int i = s-1; i >= 0; i--)
- {
- while (value >= notes[i])
- {
- value =value- notes[i];
- ans.push_back(notes[i]);
- }
- }
- for (int i = 0; i < ans.size(); i++)
- cout << ans[i] << " ";
- }
- int main()
- {
- int n;
- cout << "Your friend's amount: ";
- cin >> n;
- cout << "You will give him: ";
- findMin(n);
- return 0;
- }
- 2.//Bin packing
- #include <bits/stdc++.h>
- using namespace std;
- int nextFit(int weight[], int n, int c)
- {
- int result = 0, bin_rem = c;
- for (int i = 0; i < n; i++) {
- if (weight[i] > bin_rem) {
- result++;
- bin_rem = c - weight[i];
- }
- else
- bin_rem = bin_rem -weight[i];
- }
- return result;
- }
- int main()
- {
- int weight[] = {2, 5, 4, 7, 1, 3, 8};
- int c = 10;
- int n = sizeof(weight)/sizeof(weight[0]);
- cout << "Number of bins required: "
- << nextFit(weight, n, c);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment