Sunteți pe pagina 1din 1

#include <iostream>

#include <fstream>
using namespace std;
int n,G,val[10],v;
struct ruc
{
int g,v;
}a[50];
void citire ()
{
ifstream f("rucsac.in");
f>>n>>G;
for (int i=0;i<n;i++)
f>>a[i].g>>a[i].v;
}
void sortare ()
{
for (int i=0;i<n-1;i++)
for (int j=i;j<n;j++)
if (a[i].g / a[i].v < a[j].g / a[j].v)
swap(a[i],a[j]);
}
void prel (int &v)
{
int vt=0;
int i=0;
v=0;
while (i < n && G > 0)
{
if (a[i].g <= G)
{vt+=a[i].v;
G-=a[i].g;
val[v++]=i+1;
}
i++;
}
cout<<"Valoarea obiectelor este: "<<vt<<"\n\n";
}
void af ()
{ cout << "Obiectele din rucsac sunt : ";
for (int i=0;i<v;i++)
cout<<val[i]<<" ";
}
int main()
{
citire();
sortare();
prel(v);
af();
return 0;
}