ZOJ Problem Set - 2727 List the Books



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





分析:
题意:
三维排序问题。Name,Year,Price分别是默认的第一,第二,第三排序。但有点特殊的是:第一排序是由输入决定的。其他的两个则按默认排序的顺序。仔细分析发现:这样其实排序方法也只有三种,不妨将它们一一列出。
对于这三种排序方法,我们编写三个排序函数,根据输入选择相应的排序函数,灵活方便。
本题是对c++STL泛型编程的很好体现,STL泛型编程在这里优势明显。





ac代码:

#include <iostream>
#include<cstdio>
#include<algorithm>
#include<string>
using namespace std;
const int maxn=100+5;
string s;
struct Book
{
    string name;
    int year;
    int price;
}book[maxn];


bool cmpName(const Book &b1,const Book &b2)
{
    if(b1.name!=b2.name) return b1.name<b2.name;
    else if(b1.year!=b2.year) return b1.year<b2.year;
    else return b1.price<b2.price;
}


bool cmpYear(const Book &b1,const Book &b2)
{
    if(b1.year!=b2.year) return b1.year<b2.year;
    else if(b1.name!=b2.name) return b1.name<b2.name;
    else return b1.price<b2.price;
}


bool cmpPrice(const Book &b1,const Book &b2)
{
    if(b1.price!=b2.price) return b1.price<b2.price;
    else if(b1.name!=b2.name) return b1.name<b2.name;
    else  return b1.year<b2.year;


}


int main()
{
    int n,i,j;
    int c=0;
    while(scanf("%d",&n)&&n)
    {
        for(i=0;i<n;i++)
        cin>>book[i].name>>book[i].year>>book[i].price;
        cin>>s;
        if(s=="Name")
        sort(book,book+n,cmpName);
        else if(s=="Year")
        sort(book,book+n,cmpYear);
        else if(s=="Price")
        sort(book,book+n,cmpPrice);
        if(c)
        printf("\n");
        for(i=0;i<n;i++)
        cout<<book[i].name<<" "<<book[i].year<<" "<<book[i].price<<endl;
        c++;
    }
    return 0;
}



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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值