Advertisement
AlexandruDu

Problema Rucsacului

Dec 8th, 2020
786
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.86 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3. struct obi
  4. {
  5.     int cod,g,c;
  6.     float e,k;
  7. }v[100];
  8.  
  9. int n,GR;
  10.  
  11. void citire()
  12. {
  13.     cout<<"n=";cin>>n;
  14.     for(int i=0;i<n;i++)
  15.     {
  16.         v[i].k=0;
  17.         cout<<"v["<<i<<"]=";
  18.         v[i].cod=i;
  19.         cout<<endl<<"Greutatea ob ";cin>>v[i].g;
  20.         cout<<endl<<"Castigul ";cin>>v[i].c;
  21.         v[i].e=(float)v[i].g/v[i].c;
  22.     }
  23.     cout<<"Greutatea maxima ";cin>>GR;
  24. }
  25.  
  26. void ord()
  27. {
  28.     for(int i=0;i<n-1;i++)
  29.         for(int j=i+1;j<n;j++)
  30.             if(v[i].e>v[j].e)
  31.                 swap(v[i],v[j]);
  32. }
  33.  
  34. void greedy()
  35. {
  36.     int ok=0,i=0;
  37.     while(ok<GR)
  38.     {
  39.         ok+=v[i].g;
  40.         if(ok<=GR)
  41.            v[i].k=1;
  42.         else
  43.             {
  44.                 float x=GR-ok+v[i].g;
  45.                 v[i].k=x/v[i].g;
  46.                 ok+=x;
  47.             }
  48.         i++;
  49.     }
  50. }
  51.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement