List the Books(sort)

112 篇文章 0 订阅
47 篇文章 0 订阅

Link:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2727


ZOJ Problem Set - 2727
List the Books

Time Limit: 2 Seconds       Memory Limit: 65536 KB

Jim is fond of reading books, and he has so many books that sometimes it's hard for him to manage them. So he is asking for your help to solve this problem.

Only interest in the name, press year and price of the book, Jim wants to get a sorted list of his books, according to the sorting criteria.

Input

The problem consists of multiple test cases.

In the first line of each test case, there's an integer n that specifies the number of books Jim has. n will be a positive integer less than 100. The next n lines give the information of the books in the format Name Year PriceName will be a string consisting of at most 80 characters from alphabet, Year and Price will be positive integers. Then comes the sorting criteria, which could be NameYear or Price.

Your task is to give out the book list in ascending order according to the sorting criteria in non-ascendent order.

Note: That Name is the first criteria, Year is the second, and Price the third. It means that if the sorting criteria is Year and you got two books with the same Year, you'd sort them according to their Name. If they equals again, according to their Price. No two books will be same in all the three parameters.

Input will be terminated by a case with n = 0.

Output

For each test case, output the book list, each book in a line. In each line you should output in the format Name Year Price, the three parameters should be seperated by just ONE space.

You should output a blank line between two test cases.

Sample Input

3
LearningGNUEmacs 2003 68
TheC++StandardLibrary 2002 108
ArtificialIntelligence 2005 75
Year
4
GhostStory 2001 1
WuXiaStory 2000 2
SFStory 1999 10
WeekEnd 1998 5
Price
0

Sample Output

TheC++StandardLibrary 2002 108
LearningGNUEmacs 2003 68
ArtificialIntelligence 2005 75

GhostStory 2001 1
WuXiaStory 2000 2
WeekEnd 1998 5
SFStory 1999 10


Author:  DAI, Wenbin
Source:  Zhejiang University Local Contest 2006, Preliminary
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=200;
struct node{
	char name[maxn];
	int y;
	int p;
}book[maxn];
bool cmp1(node a,node b)
{
	if(a.y!=b.y)
	return a.y<b.y;
	else
	{
		if(strcmp(a.name,b.name)!=0)
		{
			if(strcmp(a.name,b.name)==-1)
			return true;
			else
			return false;
		}
		else
		{
			return a.p<b.p;
		}
	}
 } 
bool cmp2(node a,node b)
{
	if(a.p!=b.p)
	return a.p<b.p;
	else
	{
		if(strcmp(a.name,b.name)!=0)
		{
			if(strcmp(a.name,b.name)==-1)
			return true;
			else
			return false;
		}
		else
		{
			return a.y<b.y;
		}
	}
 } 
 bool cmp3(node a,node b)
{
	if(strcmp(a.name,b.name)!=0)
		{
			if(strcmp(a.name,b.name)==-1)
			return true;
			else
			return false;
		}
	else
	{
		if(a.y!=b.y)
		{
			return a.y<b.y;
		}
		else
		{
			return a.p<b.p;
		}
	}
 } 
int main()
{
	int n,i;
	int kase=0; 
	while(~scanf("%d",&n)&&n)
	{
		if(kase++)  
            puts("");//一开始kase值为0,不成立,所以puts不执行。之后所有的kase都非0,每次输出前,会先输出一个空行
		for(i=0;i<n;i++)
		scanf("%s%d%d",&book[i].name,&book[i].y,&book[i].p);
		char s[maxn];
		scanf("%s",s);
		if(strcmp(s,"Year")==0)
		{
			sort(book,book+n,cmp1);
			for(i=0;i<n;i++)
			printf("%s %d %d\n",book[i].name,book[i].y,book[i].p);
		}
		else if(strcmp(s,"Price")==0)
		{
			sort(book,book+n,cmp2);
			for(i=0;i<n;i++)
			printf("%s %d %d\n",book[i].name,book[i].y,book[i].p);
		}
		else
		{
			sort(book,book+n,cmp3);
			for(i=0;i<n;i++)
			printf("%s %d %d\n",book[i].name,book[i].y,book[i].p);
		}
	}
	return 0;
}


#define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAX_SIZE 100 typedef struct { int book_id; char book_name[50]; float price; } Book; typedef struct { Book books[MAX_SIZE]; int length; } BookList; void input_books(BookList* list, int n) { for (int i = 0; i < n; i++) { printf("请输入第%d本书的信息:\n", i + 1); printf("图书编号:"); scanf("%d", &list->books[i].book_id); printf("书名:"); scanf("%s", list->books[i].book_name); printf("价格:"); scanf("%f", &list->books[i].price); } list->length = n; } void display_books(BookList* list) { printf("图书表中所有图书的相关信息:\n"); for (int i = 0; i < list->length; i++) { printf("图书编号:%d\n", list->books[i].book_id); printf("书名:%s\n", list->books[i].book_name); printf("价格:%f\n", list->books[i].price); } } void insert_book(BookList* list, int pos, Book book) { if (pos < 1 || pos > list->length + 1) { printf("插入位置不合法!\n"); return; } for (int i = list->length - 1; i >= pos - 1; i--) { list->books[i + 1] = list->books[i]; } list->books[pos - 1] = book; list->length++; } void delete_book(BookList* list, int pos) { if (pos < 1 || pos > list->length) { printf("删除位置不合法!\n"); return; } for (int i = pos - 1; i < list->length - 1; i++) { list->books[i] = list->books[i + 1]; } list->length--; } int count_books(BookList* list) { return list->length; } int partition(BookList* list, int low, int high) { Book pivot = list->books[low]; while (low < high) { while (low < high && list->books[high].book_id >= pivot.book_id) high--; list->books[low] = list->books[high]; while (low < high && list->books[low].book_id <= pivot.book_id) low++; list->books[high] = list->books[low]; } list->books[low] = pivot; return low; } void quick_sort(BookList* list, int
最新发布
06-11
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

林下的码路

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值