《ACM程序设计》书中题目V

来源:互联网 发布:55gbgb最新域名获取 编辑:程序博客网 时间:2024/04/29 23:13

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 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.

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

要写3个比较函数,然后排下序,思路不是很难,就是前边代码稍微有点长

代码如下:

#include <bits/stdc++.h>using namespace std;struct book{    string name;    int year;    int price;  };bool compare_name(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 compare_year(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 compare_price(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(){    vector<book> a;    book book1;    string mainsort;    int n,i,line=0;    while(cin>>n&&n)    {        line++;        a.clear();        for(i=0;i<n;i++)        {            cin>>book1.name>>book1.year>>book1.price;            a.push_back(book1);        }        cin>>mainsort;        if(mainsort=="Name")            sort(a.begin(),a.end(),compare_name);        else if(mainsort=="Year")            sort(a.begin(),a.end(),compare_year);            else if(mainsort=="Price")                sort(a.begin(),a.end(),compare_price);        if(line!=1)            cout<<endl;        for(i=0;i<a.size();i++)        {            cout<<a[i].name<<' '<<a[i].year<<' '<<a[i].price<<endl;        }    }    return 0;}
0 0