Rachel_zky 2021-06-05 09:47 采纳率: 22.2%
浏览 131
已采纳

查最贵的书和最便宜的书

【问题描述】编写程序,从键盘输入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,price is:%0.1lf

The book with the min price:%s,price is:%0.1lf

标点符号全部为英文:

  • 写回答

1条回答 默认 最新

  • qfl_sdu 2021-06-05 14:14
    关注

    代码如下,如有帮助,请采纳一下,谢谢。

    #include <stdio.h>
    
    struct BOOK
    {
    	char name[40];
    	float price;
    };
    
    int main()
    {
    	int n,i,maxindex = 0,minindex = 0;
    	BOOK a[10];
    	printf("Input n:");
    	scanf("%d",&n);
    	for (i = 0; i < n; i++)
    	{
    		printf("Input the name,price of the %d book:",i+1);
    		scanf("%s %f",a[i].name,&a[i].price);
    		if ( i > 0)
    		{
    			if(a[i].price > a[maxindex].price)
    				maxindex = i;
    			if(a[i].price < a[minindex].price)
    				minindex = i;
    		}
    	}
    	printf("The book with the max price:%s,price is:%0.1lf\n",a[maxindex].name,a[maxindex].price);
    	printf("The book with the min price:%s,price is:%0.1lf\n",a[minindex].name,a[minindex].price);
    	return 0;
    }
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?