Run ID:73514
提交时间:2024-05-19 11:56:37
#include <iostream> #include <vector> #include <algorithm> using namespace std; struct Book { int id; string name; int quantity; int totalPrice; double unitPrice; }; bool compareByUnitPrice(const Book& a, const Book& b) { return a.unitPrice > b.unitPrice; } int main() { int n; cin >> n; // 读取书本数量 vector<Book> books(n); for (int i = 0; i < n; ++i) { cin >> books[i].id >> books[i].name >> books[i].quantity >> books[i].totalPrice; books[i].unitPrice = static_cast<double>(books[i].totalPrice) / books[i].quantity; } // 根据单价排序书籍 sort(books.begin(), books.end(), compareByUnitPrice); // 输出单价最高的书的信息 cout << books[0].id << " " << books[0].name << " " << books[0].quantity << " " << books[0].totalPrice << " " << books[0].unitPrice << endl; return 0; }