9149. 9_5 查最贵的书和最便宜的书

9149. 9_5 查最贵的书和最便宜的书

 

 

【问题描述】编写程序,从键盘输入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: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>
#include<string.h>
int main()
{

char a[10][100];
float s[10];
int i,n,max=0,min=0;
scanf("%d",&n);
for(i=0;i<n;i++)
{
scanf("%s",a[i]);
scanf("%f",&s[i]);

}

for(i=1;i<n;i++)
if(s[i]>s[max]) max=i;
for(i=1;i<n;i++)
if(s[i]<s[min]) min=i;
printf("The book with the max price:%s,%.1f\n",a[max],s[max]);
printf("The book with the min price:%s,%.1f\n",a[min],s[min]);
return 0;
}

posted @ 2020-10-16 23:43  瑜瑜子  阅读(647)  评论(0)    收藏  举报