V

来源:互联网 发布:白白发布小明永久域名 编辑:程序博客网 时间:2024/04/19 15:47

Description

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

3LearningGNUEmacs 2003 68TheC++StandardLibrary 2002 108ArtificialIntelligence 2005 75Year4GhostStory 2001 1WuXiaStory 2000 2SFStory 1999 10WeekEnd 1998 5Price0

Sample Output

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


题意:

按标准输出关于书的信息,第一标准是输入的,第二标准是按书名、年份、价格往后退的,每组数据中间空行

分析:

根据信息写个结构体,然后用sort排列

代码:

#include<bits/stdc++.h>using namespace std;struct book{    string a;    int b,c;};int com1(book q,book w){    if(q.a<w.a) return 1;    if(q.a>w.a) return 0;    if(q.b<w.b) return 1;    if(q.b>w.b) return 0;    if(q.c<w.c) return 1;    return 0;}int com2(book q,book w){    if(q.b<w.b) return 1;    if(q.b>w.b) return 0;    if(q.a<w.a) return 1;    if(q.a>w.a) return 0;    if(q.c<w.c) return 1;    return 0;}int com3(book q,book w){    if(q.c<w.c) return 1;    if(q.c>w.c) return 0;    if(q.a<w.a) return 1;    if(q.a>w.a) return 0;    if(q.b<w.b) return 1;    return 0;}int main(){    int n,i,t=0;    vector<book>v;    book s;    string f;    while(cin>>n&&n!=0)    {        if(t)            cout<<endl;        for(i=0;i<n;i++)        {            cin>>s.a>>s.b>>s.c;            v.push_back(s);        }        cin>>f;        if(f=="Year")   sort(v.begin(),v.end(),com2);        else        if(f=="Price")  sort(v.begin(),v.end(),com3);        else    sort(v.begin(),v.end(),com1);        for(i=0;i<n;i++)        cout<<v[i].a<<" "<<v[i].b<<" "<<v[i].c<<endl;        v.clear();        t=1;    }}

感受:

我感觉我写的代码,不太好,感觉有调用sort时重复的地方, 不会改快哭了

0 0
原创粉丝点击