-Cpy。 2019-12-24 11:20 采纳率: 100%
浏览 948
已采纳

求助 C语言(结构体 函数) 查找书籍

题目描述

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

输入

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

输出

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

样例输入

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

样例输出

25.0, Programming in Delphi

代码

#include"stdio.h"
struct book{
   char name[50];
   float price;
};
struct book getmax(struct book books[],int n);
int main()
{
    struct book books[20];
    @@1
    嵌入代码
    @@1
}

struct book getmax(struct book books[],int n)
@@2
嵌入代码
@@2
  • 写回答

2条回答 默认 最新

  • qtchen_1988 2019-12-24 12:00
    关注
    #include"stdio.h"
    struct book{
       char name[50];
       float price;
    };
    struct book getmax(struct book books[],int n);
    int main()
    {
        struct book books[20];
        int n,i;
        scanf("%d",&n);
        for(i=0;i<n;i++)
        {
            getchar();
            gets(books[i].name);
            scanf("%f",&books[i].price);
        }
        struct book max_book = getmax(books,n);
        printf("%0.1f,%s",max_book.price,max_book.name);
    }
    
    struct book getmax(struct book books[],int n)
    {
        int i,max_idx = 0;
        float max_p = books[0].price;
        for(i=1;i<n;i++)
        {
            if(max_p<books[i].price)
            {
                max_p = books[i].price;
                max_idx = i;
            }
        }
        return books[max_idx];
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题