#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];
}
求助 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
- 点赞
- 写回答
- 关注问题
- 收藏
- 复制链接分享
- 邀请回答