Pagini recente » Cod sursa (job #1113147) | Cod sursa (job #757026) | Rating Roberto Glont (nouseforaname) | Cod sursa (job #1160127) | Cod sursa (job #3133641)
#include<fstream>
#include<iomanip>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
struct obiect
{
float g,c;
};
void citire(float &g, int &n, obiect a[])
{
fin>>n>>g;
for(int i=1;i<=n;i++)
fin>>a[i].g>>a[i].c;
}
void ordonare(int n, obiect a[])
{
int i,j;
obiect aux;
for(i=1;i<n;i++)
for(j=i+1;j<=n;j++)
if(a[i].c<a[j].c)
{
aux=a[i]; a[i]=a[j]; a[j]=aux;
}
}
void greedy(float g, int n, obiect a[5001])
{
//obiect s[100];
int i=1;
float sp=0,castig=0;
//for(int j=1;j<=n;j++)
// fout<<j<<" "<<a[j].g<<" "<<a[j].c<<endl;
while(sp<g)
{
if(sp+a[i].g<=g)
{
sp+=a[i].g;
castig+=a[i].c;
i++;
//fout<<i<<" "<<a[i].g<<" "<<a[i].c<<endl;
}
else
{
i++;
}
}
fout<<castig;
}
int main()
{
int n;
obiect a[5001];
float g;
citire(g,n,a);
ordonare(n,a);
greedy(g,n,a);
fin.close();
fout.close();
}