`
hududumo
  • 浏览: 239111 次
文章分类
社区版块
存档分类
最新评论

ZOJ 2727List the Books

 
阅读更多
#include<fstream>
#include<iostream>
#include<vector>
#include<algorithm>
#include<string>
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
		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
		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
		return b1.Year<b2.Year;
}
int main()
{
	//ifstream cin("acmilan.txt");
	vector<Book> v;
	Book book;
	string sorting;
	int n;
	int i;
	int line=0;
	while(cin>>n)
	{
		if(n==0)
			break;
		line++;
		v.clear();
		for(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);
		else if(sorting=="Year")
			sort(v.begin(),v.end(),CompYear);
		else if(sorting=="Price")
			sort(v.begin(),v.end(),CompPrice);
		if(line!=1)
			cout<<endl;
		for(i=0;i<v.size();i++)
		{
			cout<<v[i].Name<<" "<<v[i].Year<<" "<<v[i].Price<<endl;
		}
			
	}
	//system("pause");
	return 0;
}

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics