Pagini recente » Cod sursa (job #685158) | Cod sursa (job #29114) | Cod sursa (job #1008890) | Cod sursa (job #1545198) | Cod sursa (job #2406138)
#include <stdio.h>
#include <stdlib.h>
struct obiect{
int greutate;
int cost;
int procent;
};
struct obiect a[100];
int compare(const void *e1, const void *e2){
struct obiect *o1=(struct obiect *)e1;
struct obiect *o2=(struct obiect *)e2;
if (o1->procent<o2->procent) return -1;
else if (o1->procent==o2->procent) return 0;
return 1;
}
int main()
{
FILE *fin=fopen("rucsac.in","r");
FILE *fout=fopen("rucsac.out","w");
int n,gmax;
fscanf(fin,"%d%d",&n,&gmax);
int i;
for (i=0;i<n;i++){
fscanf(fin,"%d%d",&a[i].greutate,&a[i].cost);
a[i].procent=100000*a[i].greutate*a[i].greutate/a[i].cost/gmax;
}
qsort(a,n,sizeof(struct obiect),compare);
int cost;
cost=0;
for (i=0;i<n;i++){
printf("%d %d %d\n",a[i].greutate,a[i].cost,a[i].procent);
}
for (i=0;i<n && gmax>0;i++){
if (a[i].greutate<=gmax){
gmax-=a[i].greutate;
cost+=a[i].cost;
}
}
fprintf(fout,"%d",cost);
return 0;
}