List the Books
http://acm.zju.edu.cn/show_problem.php?pid=2727
Time limit: 1 Seconds
Memory limit: 32768K
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.
1. 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 Price. Name 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 Name, Year 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.
2. 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.
3. 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
4. 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 Problem Source: Zhejiang University Local Contest 2006, Preliminary
1 /*C version
2 Written by czk*/
3 #include <stdio.h>
4
5 struct book {
6 char name[81];
7 int year;
8 int price;
9 } books[100];
10
11 int comp_price(struct book w1, struct book w2) {
12 if( w1.price - w2.price !=0)
13 return w1.price-w2.price;
14 else if(strcmp(w1.name, w2.name)!=0)
15 return strcmp(w1.name, w2.name);
16 else
17 return w1.year- w2.year;
18 }
19 int comp_name(struct book w1, struct book w2) {
20 if(strcmp(w1.name, w2.name)!=0)
21 return strcmp(w1.name, w2.name);
22 else if(w1.year != w2.year)
23 return w1.year- w2.year;
24 else
25 return w1.price - w2.price;
26 }
27
28 int comp_year(struct book w1, struct book w2) {
29 if(w1.year != w2.year)
30 return w1.year - w2.year;
31 else if(strcmp(w1.name, w2.name)!=0)
32 return strcmp(w1.name, w2.name);
33 else
34 return w1.price - w2.price;
35 }
36
37 void swap(struct book v[], int i, int j) {
38 struct book temp;
39 temp = v[i];
40 v[i] = v[j];
41 v[j] = temp;
42 }
43
44 void qsort2(struct book v[], int left, int right,
45 int (*comp)(struct book, struct book))
46 {
47 int i, last;
48
49 if (left >= right) /* do nothing if array contains */
50 return; /* fewer than two elements */
51 swap(v, left, (left + right)/2);
52 last = left;
53 for (i = left+1; i <= right; i++)
54 if ((*comp)(v[i], v[left]) < 0)
55 swap(v, ++last, i);
56 swap(v, left, last);
57 qsort2(v, left, last-1, comp);
58 qsort2(v, last+1, right, comp);
59 }
60 int main() {
61 int n;
62 int i;
63 char criteria[10];
64 scanf("%d", &n);
65 while(n!=0) {
66 for(i = 0; i < n; i++)
67 scanf("%s%d%d",books[i].name, &books[i].year, &books[i].price);
68 scanf("%s", criteria);
69 if(strcmp(criteria, "Price")==0)
70 qsort2(books, 0, n-1, comp_price);
71 else if(strcmp(criteria, "Year") == 0)
72 qsort2(books, 0, n-1, comp_year);
73 else if(strcmp(criteria, "Name") == 0)
74 qsort2(books, 0, n-1, comp_name);
75 for(i = 0; i < n; i++)
76 printf("%s %d %d\n", books[i].name,
77 books[i].year, books[i].price);
78 scanf("%d", &n);
79 if(n!=0)
80 printf("\n");
81 }
82 }