Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct data
- {
- char name;
- int weight,price;
- }element[5];
- int main()
- {
- int i=0,j,temp,cap,n,w=0;
- float ratio[5],tmp1;
- char tmp;
- scanf("%d",&n);
- printf("Enter capacity:\n");
- scanf("%d",&cap);
- for(i=0;i<n;i++)
- {
- printf("Enter weight:\n");
- scanf("%d",&element[i].weight);
- printf("Enter price:\n");
- scanf("%d",&element[i].price);
- printf("Enter name:\n");
- fflush(stdin);
- scanf("%c",&element[i].name);
- }
- for(i=0;i<n;i++)
- {
- ratio[i]=element[i].price/element[i].weight;
- }
- for (i = 0; i < n; i++)
- {
- for (j = i + 1; j <= n; j++)
- {
- if (ratio[i] < ratio[j])
- {
- tmp1 = ratio[j];
- ratio[j] = ratio[i];
- ratio[i] = tmp1;
- temp = element[j].weight;
- element[j].weight = element[i].weight;
- element[i].weight = temp;
- temp = element[j].price;
- element[j].price = element[i].price;
- element[i].price = temp;
- tmp = element[j].name;
- element[j].name = element[i].name;
- element[i].name = tmp;
- }
- }
- }
- i=0;
- while (w<=cap)
- {
- if(element[i].weight<cap-w)
- {
- printf("Name: %c Weight: %d\n",element[i].name,element[i].weight);
- w=w+element[i].weight;
- i++;
- }
- else
- {
- printf("Name: %c Over: %d\n",element[i].name,cap-w);
- break;
- }
- }
- getchar();
- getchar();
- }
Add Comment
Please, Sign In to add comment