Advertisement
Guest User

Untitled

a guest
Apr 16th, 2014
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.78 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include<locale.h>
  4. #include <windows.h>
  5. using namespace std;
  6. const int size=5;
  7. double eps=0.000005;
  8. struct conteiners
  9. {
  10. double weight;
  11. int conteiner;
  12. };
  13.  
  14. void NextFitDecreasing(conteiners weights[size],int size)
  15. {
  16. double counter=0.0;
  17. int count=1;
  18. for(int i=0; i<size; i++)
  19. {
  20. counter+=weights[i].weight;
  21. if(counter<=1.0 + eps)
  22. {
  23. weights[i].conteiner=count;
  24. }
  25. else
  26. {
  27. count++;
  28. weights[i].conteiner=count;
  29. counter=weights[i].weight;
  30. }
  31. }
  32. for(int i=0; i<size; i++)
  33. {
  34. cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
  35. }
  36.  
  37. }
  38. void FirstFitDecreasing(conteiners weights[size],int size)
  39. {
  40. int count=1;
  41.  
  42. double counter1=0.0;
  43. bool b=true;
  44. for(int i=0; i<size; i++)
  45. {
  46. b=true;
  47. for(int k=1; k<=count && b; k++)
  48. {
  49. counter1=0.0;
  50. for(int j=0; j<i; j++)
  51. {
  52. if(weights[j].conteiner==k)
  53. {
  54. counter1+=weights[j].weight;
  55. }
  56. }
  57. counter1=1.0-counter1;
  58. if(weights[i].weight<=counter1 + eps)
  59. {
  60. weights[i].conteiner=k;
  61. b=false;
  62. }
  63. }
  64. if(b)
  65. {
  66. count++;
  67. weights[i].conteiner=count;
  68. counter1=weights[i].weight;
  69. }
  70.  
  71. }
  72. for(int i=0; i<size; i++)
  73. {
  74. cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
  75. }
  76.  
  77. }
  78. void BestFitDecreasing(conteiners weights[size],int size)
  79. {
  80. int count=1;
  81. double temp[size];
  82. for(int i=0; i<size; i++)
  83. temp[i]=1.0;
  84. double counter1=0.0, free=0.0;
  85. bool b=true;
  86. for(int i=0; i<size; i++)
  87. {
  88. if(i==0) weights[i].conteiner=1;
  89. b=true;
  90. double min=1.0;
  91. int min_cont=0;
  92. for(int k=1; k<=count; k++)
  93. {
  94. counter1=0.0;
  95. for(int j=0; j<i; j++)
  96. {
  97. if(weights[j].conteiner==k)
  98. {
  99. counter1+=weights[j].weight;
  100. }
  101. }
  102. free=1.0-counter1;
  103. if(weights[i].weight<=free +eps && free>0.0)
  104. {
  105. temp[k-1]=free;
  106. b=false;
  107. }
  108. }
  109. if(!b)
  110. {
  111. for(int j=0; j<size; j++)
  112. {
  113. if(temp[j]<min)
  114. {
  115. min=temp[j];
  116. min_cont=j;
  117. min_cont++;
  118. weights[i].conteiner=min_cont;
  119. }
  120. }
  121. }
  122. else
  123. {
  124. count++;
  125. weights[i].conteiner=count;
  126. }
  127. for(int j=0; j<size; j++)
  128. temp[j]=1.0;
  129. }
  130. for(int i=0; i<size; i++)
  131. {
  132. cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
  133. }
  134.  
  135. }
  136. int main()
  137. {
  138. setlocale(0, "");
  139. conteiners weights[size];
  140. ifstream fin("input.txt");
  141. double value;
  142. for(int i=0; i<size; ++i)
  143. {
  144. fin>>value;
  145. weights[i].weight=value;
  146. }
  147. for(int i=0; i<size; i++)
  148. weights[i].conteiner=0;
  149. cout<<"\nNextFitDecreasing :\n" <<endl;
  150. NextFitDecreasing(weights,size);
  151. for(int i=0; i<size; i++)
  152. weights[i].conteiner=0;
  153. cout<<"\n\nFirstFitDecreasing :\n" <<endl;
  154. FirstFitDecreasing(weights,size);
  155. for(int i=0; i<size; i++)
  156. weights[i].conteiner=0;
  157. cout<<"\n\nBestFitDecreasing :\n" <<endl;
  158. BestFitDecreasing(weights,size);
  159.  
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement