Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct
- {
- int id;
- double greutate, castig;
- }Obiect;
- int cmpObiecte(const void *a, const void *b) // ord descr
- {
- Obiect va = *(Obiect *)a;
- Obiect vb = *(Obiect *)b;
- if (va.castig /va.greutate < vb.castig / vb.greutate)
- return 1;
- if (va.castig /va.greutate > vb.castig / vb.greutate)
- return 0;
- }
- int main()
- {
- int n,i;
- double G, ct, auxG, p;
- Obiect *ob;
- FILE *fin, *fout;
- fin = fopen("rucsac.in","r");
- fscanf(fin,"%d",&G);
- fscanf(fin,"%d",&n);
- ob = (Obiect*) malloc(n*sizeof(Obiect));
- for (i=0; i<n; i++)
- {
- ob[i].id = i+1;
- fscanf(fin,"%d %d",&ob[i].castig,&ob[i].greutate)
- }
- fclose(fin);
- qsort(ob,n,sizeof(Obiect),cmpObiecte);
- auxG = G;
- ct = 0;
- for (i=0; i<n; i++)
- if(ob[i].greutate <= auxG)
- {
- printf("obiectul %d: 100%%\n",ob[i].id);
- ct = ct + ob[i].castig;
- auxG = auxG - ob[i].greutate;
- }
- else
- {
- p = auxG/ob[i].greutate;
- printf("obiectul %d: %.2f%%\n",ob[i].id,p*100);
- ct = ct + p * ob[i].castig;
- auxG = 0;
- break;
- }
- printf("Castigul total este: %d\n",ct);
- fclose(fout);
- free(ob);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement