题目要求:
给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。
输入格式:
输入第一行给出正整数n(<10),随后给出n本书的信息。每本书在一行中给出书名,即长度不超过30的字符串,随后一行中给出正实数价格。题目保证没有同样价格的书。
输出格式:
在一行中按照“价格, 书名”的格式先后输出价格最高和最低的书。价格保留2位小数。
输入样例1:
3
Programming in C
21.5
Programming in VB
18.5
Programming in Delphi
25.0
输出样例1:
25.00, Programming in Delphi
18.50, Programming in VB
代码:
#include <stdio.h>
struct book
{
char name[100];
double money;
};
int main()
{
int n, i, max_index, min_index;
struct book books[100];
double max, min;
scanf("%d", &n);
for(i=0; i<n; i++)
{
getchar();
gets(books[i].name);
scanf("%lf", &books[i].money);
}
max = books[0].money;
min = books[0].money;
for(i=0; i<n; i++)
{
if(books[i].money>=max)
{
max_index = i;
}
if(books[i].money<=min)
{
min_index = i;
}
}
printf("%.2f, %s\n%.2f, %s", books[max_index].money, books[max_index].name, books[min_index].money, books[min_index].name);
return 0;
}