c++给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价
时间: 2024-06-03 22:04:37 浏览: 147
好的,这个问题可以使用C++语言编写程序来解决。以下是一个简单的示例代码,可以帮助您实现此功能:
```
#include <iostream>
#include <string>
using namespace std;
struct Book {
string name;
double price;
};
int main() {
int n;
cout << "请输入书籍的数量:";
cin >> n;
Book books[n];
for (int i = 0; i < n; i++) {
cout << "请输入第" << i+1 << "本书的名称和价格:";
cin >> books[i].name >> books[i].price;
}
double maxPrice = books.price, minPrice = books.price;
string maxName = books.name, minName = books.name;
for (int i = 1; i < n; i++) {
if (books[i].price > maxPrice) {
maxPrice = books[i].price;
maxName = books[i].name;
}
if (books[i].price < minPrice) {
minPrice = books[i].price;
minName = books[i].name;
}
}
cout << "价格最高的书籍是:" << maxName << ",价格为:" << maxPrice << endl;
cout << "价格最低的书籍是:" << minName << ",价格为:" << minPrice << endl;
// 相关问题
cout << "
阅读全文