Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- struct obi
- {
- int cod,g,c;
- float e,k;
- }v[100];
- int n,GR;
- void citire()
- {
- cout<<"n=";cin>>n;
- for(int i=0;i<n;i++)
- {
- v[i].k=0;
- cout<<"v["<<i<<"]=";
- v[i].cod=i;
- cout<<endl<<"Greutatea ob ";cin>>v[i].g;
- cout<<endl<<"Castigul ";cin>>v[i].c;
- v[i].e=(float)v[i].g/v[i].c;
- }
- cout<<"Greutatea maxima ";cin>>GR;
- }
- void ord()
- {
- for(int i=0;i<n-1;i++)
- for(int j=i+1;j<n;j++)
- if(v[i].e>v[j].e)
- swap(v[i],v[j]);
- }
- void greedy()
- {
- int ok=0,i=0;
- while(ok<GR)
- {
- ok+=v[i].g;
- if(ok<=GR)
- v[i].k=1;
- else
- {
- float x=GR-ok+v[i].g;
- v[i].k=x/v[i].g;
- ok=ok+x-v[i].g;
- }
- i++;
- }
- }
- void afisare()
- { float cs=0;
- for(int i=0;i<n;i++)
- if(v[i].k!=0)
- {cout<<endl<<v[i].cod<<" "<<v[i].k<<" "<<v[i].e;
- cs+=v[i].c*v[i].k;}
- cout<<βcastig total=β<<cs;
- }
- int main()
- {
- citire();
- ord();
- greedy();
- afisare();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement