每日一题Day15

基于链式存储结构的图书信息表的最贵图书的查找

描述

定义一个包含图书信息(书号、书名、价格)的链表,读入相应的图书数据来完成图书信息表的创建,然后查找价格最高的图书,输出相应图书的信息。

输入

总计输入n+1行,其中,第一行是图书数目n,后n行是n本图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,价格之后没有空格。其中书号和书名为字符串类型,价格为浮点数类型。

输出

总计输出m+1行,其中,第一行是最贵图书数目(价格最高的图书可能有多本),后m行是最贵图书的信息(书号、书名、价格),每本图书信息占一行,书号、书名、价格用空格分隔,其中价格输出保留两位小数。

样例输入1 

8
9787302257646 Data-Structure 35.00
9787302164340 Operating-System 50.00
9787302219972 Software-Engineer 32.00
9787302203513 Database-Principles 36.00
9787810827430 Discrete-Mathematics 36.00
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00
9787822234110 The-C-Programming-Language 38.00

样例输出1

2
9787302257800 Data-Structure 62.00
9787811234923 Compiler-Principles 62.00

解答:尾插法顺序建立带头结点的链表,同时记录最大值。遍历链表统计最大值个数,输出相应信息。

#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
	long long int num;
	char name[50];
	double price;
	struct node *next;
} Book,*BookList;

int main()
{
	int n;
	scanf("%d",&n);
	BookList L;
	Book *p,*rear;
	L = (Book *)malloc(sizeof(Book));
	L->num=n;
	L->next=NULL;
	rear = L;
	double max=0;
	while(n--)
	{
		p=(Book *)malloc(sizeof(Book));
		scanf("%lld %s %lf",&p->num,p->name,&p->price);
		if(p->price-max>1e-6)
		{
			max=p->price;
		}
		rear->next = p;
		rear = p;
	}
	rear->next = NULL;
	int count=0;
	p=L->next;
	while(p)
	{
		if(max-p->price<1e-6)
		{
			count++;
		}
		p=p->next;
	}
	printf("%d\n",count);
	p=L->next;
	while(p)
	{
		if(max-p->price<1e-6)
		{
			printf("%lld %s %.2f\n",p->num,p->name,p->price);
		}
		p=p->next;
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/ZLambert/article/details/81540356