Pagini recente » Cod sursa (job #2617403) | Cod sursa (job #2433487) | Cod sursa (job #3222972) | Cod sursa (job #2365079) | Cod sursa (job #2750746)
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef struct
{
int greutate, valoare;
} obiect;
int fcmpq(const void *a, const void *b)
{
obiect *A = (obiect *)a;
obiect *B = (obiect *)b;
double eff1 = 1.0 * A->valoare / A->greutate - 1.0 * B->valoare / B->greutate;
// printf("%d %d\n", A->greutate, A->valoare);
// printf("%d %d\n", B->greutate, B->valoare);
return -(int)eff1 * 100;
}
int main()
{
FILE *fin = fopen("rucsac.in", "r");
FILE *fout = fopen("rucsac.out", "w");
int n = 0, Gmax = 0;
obiect *O = (obiect *)calloc(n, sizeof(obiect));
fscanf(fin, "%d%d", &n, &Gmax);
for (int i = 0; i < n; ++i)
fscanf(fin, "%d%d", &O[i].greutate, &O[i].valoare);
qsort(O, n, sizeof(obiect), fcmpq);
int G = 0;
double V = 0;
for (int i = 0; i < n; i++)
{
if (G + O[i].greutate <= Gmax)
{
G += O[i].greutate;
V += O[i].valoare;
}
else if (Gmax > G)
{
double x = 1.0 * (Gmax - G) / O[i].greutate;
G = Gmax;
V += x * O[i].valoare;
}
else
i = n + 5;
}
fprintf(fout, "%lf", V);
// printf("%lf", V);
return 0;
}