Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<fstream>
- #include<iostream>
- using namespace std;
- struct obiect
- { float g,c,e;
- int k;
- };
- obiect a[20];
- int n,m,s[20],G=20;
- float x[20];
- void citire()
- { ifstream f("e:\\info\\greedy\\rucsac.txt");
- f>>n;
- for(int i=1;i<=n;i++)
- { f>>a[i].k>>a[i].g>>a[i].c;
- a[i].e=a[i].c/a[i].g;}
- f.close();}
- void afisare()
- { for(int i=1;i<=n;i++)
- { cout<<a[i].k<<" "<<a[i].g<<" "<<a[i].c<<" "<<a[i].e;
- cout<<endl;}}
- void sortare()
- { obiect aux;
- for(int i=1;i<n;i++)
- for(int j=i+1;j<=n;j++)
- if(a[i].e<a[j].e)
- { aux=a[i];
- a[i]=a[j];
- a[j]=aux;}}
- void greedy()
- { int Gr=G,j=0;
- for(int i=1;i<=n && Gr!=0;i++)
- if(Gr>a[i].g)
- { j++;
- s[j]=i;
- x[j]=1;
- Gr=Gr-a[i].g;}
- else
- { j++;
- s[j]=i;
- x[j]=Gr/a[i].g;
- Gr=0;}
- m=j;
- cout<<m;}
- void afisareS()
- { for(int i=1;i<=m;i++)
- cout<<"obiectul nr "<<a[s[i]].k<<" in cantitatea "<<x[i]*a[s[i]].g<<endl;}
- int main ()
- { citire();
- afisare();
- sortare();
- cout<<endl;
- afisare();
- greedy();
- cout<<endl;
- afisareS();}
- 5
- 1 5 10
- 2 4 20
- 3 4 10
- 4 8 10
- 5 10 22
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement