Untitled
plain_text
a month ago
1.3 kB
6
Indexable
Never
#include <bits/stdc++.h> using namespace std; struct Item { string name; double price, weight, unitPrice; }; bool comp(Item i1, Item i2) { return (i1.unitPrice > i2.unitPrice); } int main() { int n; cout << "enter the number of items: "; cin >> n; Item item[n]; for (int i = 0; i < n; i++) { cout << "item name:"; cin >> item[i].name; cout << "item price:"; cin >> item[i].price; cout << "item weight:"; cin >> item[i].weight; item[i].unitPrice = (item[i].price / item[i].weight); } double cap; cout << "capacity:"; cin >> cap; sort(item, item + n, comp); for (int i = 0; i < n; i++) { cout << "Item name:" << item[i].name << " price:" << item[i].price << " weight:" << item[i].weight << " unit price:" << item[i].unitPrice << endl; } double profit = 0; for (int i = 0; i < n; i++) { if (cap >= item[i].weight) { profit = profit + item[i].price; cap -= item[i].weight; } else { profit = profit + (item[i].unitPrice * cap); cap=0; } } cout << "profit: " << profit << endl; return 0; }