Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.26 KB | None | 0 0
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. int firstFit(int weight[], int n, int c)
  7. {
  8. int res = 0;
  9.  
  10. int bin_rem[n];
  11.  
  12. for (int i = 0; i < n; i++) {
  13. int j;
  14. for (j = 0; j < res; j++) {
  15. if (bin_rem[j] >= weight[i]) {
  16. bin_rem[j] = bin_rem[j] - weight[i];
  17. break;
  18. }
  19. }
  20.  
  21. if (j == res) {
  22. bin_rem[res] = c - weight[i];
  23.  
  24. res++;
  25. }
  26.  
  27. }
  28. for(int i=0;i<res;i++)
  29. cout << bin_rem[i] << endl;
  30.  
  31. return res;
  32. }
  33. int firstFitDec(int weight[], int n, int c)
  34. {
  35. sort(weight, weight + n, std::greater<int>());
  36. return firstFit(weight, n, c);
  37. }
  38.  
  39. int main()
  40. {
  41.  
  42. cout << "Iveskite daiktu kieki: ";
  43. int n;
  44. cin >> n;
  45. cout << "Iveskite " << n << " daiktu turius:";
  46. int weight[n];
  47. for (int i = 0; i < n; i++)
  48. cin >> weight[i];
  49. cout << "Iveskite dezutes turi: ";
  50. int size;
  51. cin >> size;
  52. //int weight[] = {9,4,6,8,1,4,6,5,3,3,3};
  53. //int c = 10;
  54. //int n = sizeof(weight) / sizeof(weight[0]);
  55.  
  56. cout << "Maziausias dezuciu skaicius: " << firstFitDec(weight, n, size);
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement