Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <fstream>
- #include<locale.h>
- #include <windows.h>
- using namespace std;
- const int size=5;
- double eps=0.000005;
- struct conteiners
- {
- double weight;
- int conteiner;
- };
- void NextFitDecreasing(conteiners weights[size],int size)
- {
- double counter=0.0;
- int count=1;
- for(int i=0; i<size; i++)
- {
- counter+=weights[i].weight;
- if(counter<=1.0 + eps)
- {
- weights[i].conteiner=count;
- }
- else
- {
- count++;
- weights[i].conteiner=count;
- counter=weights[i].weight;
- }
- }
- for(int i=0; i<size; i++)
- {
- cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
- }
- }
- void FirstFitDecreasing(conteiners weights[size],int size)
- {
- int count=1;
- double counter1=0.0;
- bool b=true;
- for(int i=0; i<size; i++)
- {
- b=true;
- for(int k=1; k<=count && b; k++)
- {
- counter1=0.0;
- for(int j=0; j<i; j++)
- {
- if(weights[j].conteiner==k)
- {
- counter1+=weights[j].weight;
- }
- }
- counter1=1.0-counter1;
- if(weights[i].weight<=counter1 + eps)
- {
- weights[i].conteiner=k;
- b=false;
- }
- }
- if(b)
- {
- count++;
- weights[i].conteiner=count;
- counter1=weights[i].weight;
- }
- }
- for(int i=0; i<size; i++)
- {
- cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
- }
- }
- void BestFitDecreasing(conteiners weights[size],int size)
- {
- int count=1;
- double temp[size];
- for(int i=0; i<size; i++)
- temp[i]=1.0;
- double counter1=0.0, free=0.0;
- bool b=true;
- for(int i=0; i<size; i++)
- {
- if(i==0) weights[i].conteiner=1;
- b=true;
- double min=1.0;
- int min_cont=0;
- for(int k=1; k<=count; k++)
- {
- counter1=0.0;
- for(int j=0; j<i; j++)
- {
- if(weights[j].conteiner==k)
- {
- counter1+=weights[j].weight;
- }
- }
- free=1.0-counter1;
- if(weights[i].weight<=free +eps && free>0.0)
- {
- temp[k-1]=free;
- b=false;
- }
- }
- if(!b)
- {
- for(int j=0; j<size; j++)
- {
- if(temp[j]<min)
- {
- min=temp[j];
- min_cont=j;
- min_cont++;
- weights[i].conteiner=min_cont;
- }
- }
- }
- else
- {
- count++;
- weights[i].conteiner=count;
- }
- for(int j=0; j<size; j++)
- temp[j]=1.0;
- }
- for(int i=0; i<size; i++)
- {
- cout<<"Контейнер "<<weights[i].conteiner<<" : "<<weights[i].weight<<endl;
- }
- }
- int main()
- {
- setlocale(0, "");
- conteiners weights[size];
- ifstream fin("input.txt");
- double value;
- for(int i=0; i<size; ++i)
- {
- fin>>value;
- weights[i].weight=value;
- }
- for(int i=0; i<size; i++)
- weights[i].conteiner=0;
- cout<<"\nNextFitDecreasing :\n" <<endl;
- NextFitDecreasing(weights,size);
- for(int i=0; i<size; i++)
- weights[i].conteiner=0;
- cout<<"\n\nFirstFitDecreasing :\n" <<endl;
- FirstFitDecreasing(weights,size);
- for(int i=0; i<size; i++)
- weights[i].conteiner=0;
- cout<<"\n\nBestFitDecreasing :\n" <<endl;
- BestFitDecreasing(weights,size);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement