给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。

给定n本书的名称和定价,本题要求编写程序,查找并输出其中定价最高和最低的书的名称和定价。

输入格式:

输入第一行给出正整数n(<10),随后给出n本书的信息。每本书在一行中给出书名,即长度不超过30的字符串,随后一行中给出正实数价格。题目保证没有同样价格的书。

输出格式:

在一行中按照“价格, 书名”的格式先后输出价格最高和最低的书。价格保留2位小数。

输入样例:

3
Programming in C
21.5
Programming in VB
18.5
Programming in Delphi
25.0

输出样例:

25.00, Programming in Delphi
18.50, Programming in VB
#include <stdio.h>

struct book{
    char name[31];  // 书名不超过30字符
    double price;
};

int main(){
    int n, i, index_Max = 0 , index_Min = 0;
    struct book books[10]; 
    scanf("%d", &n);
    getchar();  // 从缓冲区读走回车 
    
    for( i=0; i<n; i++ ){
        gets(books[i].name);
        scanf(" %lf", &books[i].price);
        getchar();  // 从缓冲区读走回车 
    }
    
    for( i=1; i<n; i++ ){
        if( books[i].price > books[index_Max].price )
            index_Max = i;
        if( books[i].price < books[index_Min].price )
            index_Min = i;
    }
    
    printf("%.2f, %s\n", books[index_Max].price, books[index_Max].name);
    printf("%.2f, %s", books[index_Min].price, books[index_Min].name);
    
    return 0;
}

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 5
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值