【程序填空题】查最贵的书和最便宜的书。
【问题描述】
编写程序,从键盘输入n(n<10)种书的名称和定价并存入结构体数组中,从中查找定价最高及最低的书名和定价,并输出。
【输入形式】
先输入书总数n(整型,n<10),再依次输入每种书的名称(字符串)和定价。
【输出形式】
输出定价最高及最低的书名和定价。
【样例输入】(下画线部分表示输入)
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:Python 25.0
【样例输出】
The book with the max price:Python,25.0
The book with the min price:VB,18.5
【样例说明】
输出定价最高的书名和定价,再输出定价最低的书名和定价,格式为:
The book with the max price:%s,%.1f
The book with the min price:%s,%.1f
程序填空部分:
#include <stdio.h>
struct book {
char name[20];
float price;
}a[10];
int main()
{
int i,n,max=0,min=0;
printf("Input n:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("Input the name,price of the %d book:", (1) );
scanf("%s%f", (2) ,&a[i].price);
}
for(i=1;i<n;i++)
{
if(a[i].price > a[max].price)
(3) ;
if(a[i].price < (4) )
min=i;
}
printf("The book with the max price:%s,%.1f\n", (5) ,a[max].price);
printf("The book with the min price:%s,%.1f",a[min].name,a[min].price);
return 0;
}
正确答案:
第一空:
i+1
第二空:
a[i].name
第三空:
max=i
第四空:
a[min].price
第五空:
a[max].name