Cod sursa(job #3133634)

Utilizator Muntean_Vlad_AndreiMuntean Vlad Andrei Muntean_Vlad_Andrei Data 26 mai 2023 14:30:51
Problema Problema rucsacului Scor 0
Compilator cpp-64 Status done
Runda Arhiva educationala Marime 1.06 kb
#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();
}