Pagini recente » Cod sursa (job #2365171) | Cod sursa (job #1935385) | Cod sursa (job #2999224) | Cod sursa (job #1822342) | Cod sursa (job #869251)
Cod sursa(job #869251)
#include <fstream>
//problema rucsacului
using namespace std;
float nidalee[100],p[100],w[100];
int main()
{
double profi=0,n,g,aux;
ifstream f("rucsac.in");
ofstream lol("rucsac.out");
f>>n>>g;
for(int i=1;i<=n;i++)
{
f>>w[i]>>p[i];
nidalee[i]=p[i]/w[i];
}
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
if(nidalee[i]<nidalee[j])
{
aux=nidalee[i];
nidalee[i]=nidalee[j];
nidalee[j]=aux;
aux=w[i];
w[i]=w[j];
w[j]=aux;
aux=p[i];
p[i]=p[j];
p[j]=aux;
}
}
}
for(int i=1;i<=n;i++)
{
if(g>w[i])
{
profi+=p[i];
g-=w[i];
}
}
lol<<profi<<" "<<g<<endl;
for(int i=1;i<=n;i++)
lol<<nidalee[i]<<" ";
}