Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <algorithm>
- using namespace std;
- int firstFit(int weight[], int n, int c)
- {
- int res = 0;
- int bin_rem[n];
- for (int i = 0; i < n; i++) {
- int j;
- for (j = 0; j < res; j++) {
- if (bin_rem[j] >= weight[i]) {
- bin_rem[j] = bin_rem[j] - weight[i];
- break;
- }
- }
- if (j == res) {
- bin_rem[res] = c - weight[i];
- res++;
- }
- }
- for(int i=0;i<res;i++)
- cout << bin_rem[i] << endl;
- return res;
- }
- int firstFitDec(int weight[], int n, int c)
- {
- sort(weight, weight + n, std::greater<int>());
- return firstFit(weight, n, c);
- }
- int main()
- {
- cout << "Iveskite daiktu kieki: ";
- int n;
- cin >> n;
- cout << "Iveskite " << n << " daiktu turius:";
- int weight[n];
- for (int i = 0; i < n; i++)
- cin >> weight[i];
- cout << "Iveskite dezutes turi: ";
- int size;
- cin >> size;
- //int weight[] = {9,4,6,8,1,4,6,5,3,3,3};
- //int c = 10;
- //int n = sizeof(weight) / sizeof(weight[0]);
- cout << "Maziausias dezuciu skaicius: " << firstFitDec(weight, n, size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement