zoj 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
 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <algorithm>
 5 using namespace std;
 6 struct book{
 7     string name;
 8     int year;
 9     int price;
10 };
11 bool cmpName(const book &a, const book &b){
12     if(a.name != b.name) 
13         return a.name < b.name;
14     else if(a.year != b.year)
15         return a.year < b.year;
16     else
17         return a.price < b.price;
18 }
19 
20 bool cmpYear(const book &a, const book &b){
21     if(a.year != b.year)
22         return a.year < b.year;
23     else if(a.name != b.name)
24         return a.name < b.name;
25     else
26         return a.price < b.price;
27 }
28 
29 bool cmpPrice(const book &a, const book &b){
30     if(a.price != b.price)
31         return a.price < b.price;
32     else if (a.name != b.name)
33         return a.name < b.name;
34     else
35         return a.year < b.year;
36 }
37 
38 int main(){
39     int n;
40     vector<book> v;
41     book bk;
42     string sorting;
43     int line = 0;
44     while(cin >> n){
45         if(n == 0)
46             break;
47         v.clear();
48         line++;
49         for(int i = 0; i < n; i++){
50             cin >> bk.name >> bk.year >> bk.price;
51             v.push_back(bk);
52         }
53         cin >> sorting;
54         if(sorting == "Name")
55             sort(v.begin(), v.end(), cmpName);
56         else if (sorting == "Year")
57             sort(v.begin(), v.end(), cmpYear);
58         else
59             sort(v.begin(), v.end(), cmpPrice);
60         if(line != 1)
61             cout << endl;
62         for(int i = 0; i < n; i++)
63             cout << v[i].name << " " << v[i].year << " " << v[i].price << endl;
64     }
65     return 0;
66 }

 

 
posted @ 2017-03-09 21:14  琴影  阅读(338)  评论(0编辑  收藏  举报