#include <stdio.h>
#include <stdlib.h>
typedef struct {
char name[50];
float weight;
float value;
int divisible; // 1 = divisible, 0 = indivisible
float ratio; // value-to-weight ratio
} Item;
void sortByRatio(Item items[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
if (items[j].ratio < items[j + 1].ratio) {
Item temp = items[j];
items[j] = items[j + 1];
items[j + 1] = temp;
}
}
}
}
int main() {
int n;
float W;
printf("Enter number of relief items: ");
scanf("%d", &n);
Item items[n];
for (int i = 0; i < n; i++) {
printf("\nEnter name of item %d: ", i + 1);
scanf("%s", items[i].name);
printf("Enter weight (kg): ");
scanf("%f", &items[i].weight);
printf("Enter utility value: ");
scanf("%f", &items[i].value);
printf("Is the item divisible? (1 = Yes, 0 = No): ");
scanf("%d", &items[i].divisible);
items[i].ratio = items[i].value / items[i].weight;
}
printf("\nEnter maximum weight capacity of the boat (kg): ");
scanf("%f", &W);
sortByRatio(items, n);
float totalValue = 0.0, totalWeight = 0.0;
printf("\nItems selected for transport:\n");
printf("%-15s %-10s %-10s %-10s %-10s\n", "Name", "Weight", "Value", "Taken(kg)", "TotalVal");
for (int i = 0; i < n; i++) {
if (totalWeight + items[i].weight <= W) {
// Take whole item
totalWeight += items[i].weight;
totalValue += items[i].value;
printf("%-15s %-10.2f %-10.2f %-10.2f %-10.2f\n",
items[i].name, items[i].weight, items[i].value, items[i].weight, items[i].value);
} else {
// Take fraction if divisible
if (items[i].divisible) {
float remain = W - totalWeight;
float fractionValue = items[i].ratio * remain;
totalWeight += remain;
totalValue += fractionValue;
printf("%-15s %-10.2f %-10.2f %-10.2f %-10.2f\n",
items[i].name, items[i].weight, items[i].value, remain, fractionValue);
}
break; // Can't take any more items
}
}
printf("\nTotal weight carried: %.2f kg\n", totalWeight);
printf("Total utility value: %.2f\n", totalValue);
return 0;
}