Cod sursa(job #1005600)

Utilizator gigiibirjoBirjovanu Georgiana gigiibirjo Data 5 octombrie 2013 12:48:37
Problema Problema rucsacului Scor 0
Compilator cpp Status done
Runda Arhiva educationala Marime 0.99 kb
#include<iostream>
#include<fstream>
using namespace std;
ifstream fin("rucsac.in");
ofstream fout("rucsac.out");
int n, G;
struct
{
	int greut, pret;
	float profit;
} v[100],aux;
void citire()
{
	int i;
	fin>>n>>G;
	for(i=1; i<=n; i++)
	{
		fin>>v[i].greut>>v[i].pret;
		v[i].profit=(float) v[i].pret/v[i].greut;
	}
	fin.close();
} 
void ordo()
{
	int i, ok;
	do
	{
		ok=0;
		for(i=1; i<=n-1; i++)
			if(v[i].profit<v[i+1].profit)
			{
				aux=v[i];
				v[i]=v[i+1];
				v[i+1]=aux;
				ok=1;
			}
			else if(v[i].profit==v[i+1].profit && v[i].greut>v[i+1].greut)
			{
				aux=v[i];
				v[i]=v[i+1];
				v[i+1]=aux;
				ok=1;
			}
	}
	while(ok);
}
void Greedy()
{
	int i, Pmax=0;
	for(i=1; i<=n && G>0; i++)
		if(v[i].greut<=G)
		{
			Pmax=Pmax+v[i].pret;
			G=G-v[i].greut;
		}
	fout<<Pmax<<" ";
}
int main()
{
	citire();
	ordo();
	Greedy();
	int i;
	for(i=1; i<=n; i++)
		fout<<v[i].greut<<" "<<v[i].pret<<" "<<v[i].profit<<"\n";
	return 0;
}