您的位置:首页 > 其它

ZOJ 2727 List the Books

2013-03-08 15:40 309 查看
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1727

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;

struct Book
{
string Name;
int Year;
int Price;
};
bool CompName(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 if(b1.Price!=b2.Price) return b1.Price<b2.Price;
}
bool CompYear(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 if(b1.Price!=b2.Price) return b1.Price<b2.Price;
}
bool CompPrice(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 if(b1.Year!=b2.Year) return b1.Year<b2.Year;
}
int main()
{
vector<Book>v;
Book book;
string sorting;
int line=0;
int n;
while(cin>>n&&n)
{
line++;
v.clear();
for(int i=0;i<n;i++)
{
cin>>book.Name>>book.Year>>book.Price;
v.push_back(book);
}
cin>>sorting;
if(sorting=="Name")
sort(v.begin(),v.end(),CompName);
if(sorting=="Year")
sort(v.begin(),v.end(),CompYear);
if(sorting=="Price")
sort(v.begin(),v.end(),CompPrice);
if(line!=1) cout<<endl;
for(int i=0;i<v.size();i++)
cout<<v[i].Name<<" "<<v[i].Year<<" "<<v[i].Price<<endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: