查找单价最高和最低的书籍
#include <cstdio>
#include <string>
#include <iostream>
using namespace std;
struct Book {
char Book[1000];
//string Book;
float Price;
} books[10];
int main() {
int n;
scanf("%d", &n);
if (n == 0) return 0;
for (int i = 0; i < n; i++) {
getchar();
cin.getline(books[i].Book, sizeof(books[i].Book));
//cin.getline中的字符类型为char数组
//cin.getline(char数组, 读取的长度);
//geline(cin, string类型);
scanf("%f", &books[i].Price);
}
int max = 0, min = 0;
for (int i = 0; i < n; i++) {
if (books[i].Price > books[max].Price) {
max = i;
}
if (books[i].Price < books[min].Price) {
min = i;
}
}
printf("highest price: %.1f, %s\n", books[max].Price, books[max].Book);
//使用getline时
//用printf输出时%s对应的是books[max].Book.c_str()
printf("lowest price: %.1f, %s\n", books[min].Price, books[min].Book);
return 0;
}