【问题描述】编写程序,从键盘输入n本书的名称和定价并存入链表中,从中查找定价最高和最低的书的名称和定价,并输出。
【输入形式】先输入书本数n(整型),再依次输入每本书的名称(字符串)和定价(实型)。
【输入输出样例】(下划线部分表示输入)
Input n:3
Input the name,price of the 1 book:C 21.5
Input the name,price of the 2 book:VB 18.5
Input the name,price of the 3 book:Delphi 25.0
The book with the max price:Delphi,price is:25.0
The book with the min price:VB,price is:18.5
【样例说明】
输出价格最高的书的名称和定价,再输出价格最低的书的名称和定价,格式为
The book with the max price:%s,%.1f
The book with the min price:%s,%.1f
标点符号全部为英文
#include <stdio.h>
struct mess {
char name[100];
float price;
} mes[1024];
int main() {
int i, n, x, y;
scanf("%d", &n);
float max = 0, min = 1024;
for (i = 0; i < n; i++) {
scanf("%s", &mes[i].name);
scanf("%f", &mes[i].price);
}
for (i = 0; i < n; i++) {
if (max < mes[i].price) {
max = mes[i].price;
x = i;
}
if (min > mes[i].price) {
min = mes[i].price;
y = i;
}
}
printf("Input n:");
for(i=0;i<n;i++){
printf("Input the name,price of the %d book:",i+1);
}
printf("The book with the max price:%s,%.1f\n", mes[x].price, mes[x].name);
printf("The book with the min price:%s,%.1f", mes[y].price, mes[y].name);
}