ZOJ List the Books 水~
2014-01-02 21:54
309 查看
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1727
题目大意:
给你书名、出版时间、价格,让你按照一定的顺序排序。。
其中题目会给出优先级最高的,剩下两个按书名优先级>出版时间>价格来排序。
思路:
练习sort重载的。。。
不过sort(book,book+n,cmp_by_name);的重载函数竟然不能用引用。。不然会cp好吧,我习惯写重载<运算符了。
题目大意:
给你书名、出版时间、价格,让你按照一定的顺序排序。。
其中题目会给出优先级最高的,剩下两个按书名优先级>出版时间>价格来排序。
思路:
练习sort重载的。。。
不过sort(book,book+n,cmp_by_name);的重载函数竟然不能用引用。。不然会cp好吧,我习惯写重载<运算符了。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int MAXN=100+2; struct Book { char name[MAXN]; int year,price; }book[MAXN]; bool cmp_by_name(Book a,Book b) { int k=strcmp(a.name,b.name); if(k < 0) return true; else if(k > 0) return false; return a.year < b.year || (a.year==b.year && a.price<b.price); } bool cmp_by_price(Book a,Book b) { if(a.price < b.price) return true; else if(a.price > b.price) return false; int k=strcmp(a.name,b.name); if(k < 0) return true; else if(k > 0) return false; return a.year<b.year; } bool cmp_by_year(Book a,Book b) { if(a.year < b.year) return true; else if(a.year > b.year) return false; int k=strcmp(a.name,b.name); if(k < 0) return true; else if(k > 0) return false; return a.price<b.price; } int main() { int n; int kase=0; while(~scanf("%d",&n),n) { if(kase++) puts(""); for(int i=0;i<n;i++) scanf("%s%d%d",book[i].name,&book[i].year,&book[i].price); char cmd[20]; scanf("%s",cmd); if(strcmp(cmd,"Name")==0) sort(book,book+n,cmp_by_name); else if(strcmp(cmd,"Year")==0) sort(book,book+n,cmp_by_year); else if(strcmp(cmd,"Price")==0) sort(book,book+n,cmp_by_price); for(int i=0;i<n;i++) printf("%s %d %d\n",book[i].name,book[i].year,book[i].price); } return 0; }
相关文章推荐
- zoj 2727 List the Books(排序)
- zoj 2727 List the Books(三级排序 = =水题)
- zoj 2727 list the books
- zoj 2727 List the Books
- ZOJ 2727 List the Books
- ZOJ List the Books 水~
- ZOJ2727 List the Books
- 转载:The Essential List of JavaScript, jQuery, HTML & CSS Books to Make You a Better Web Developer
- A list of books on general-purpose algorithms for the practical programmer (or 'software engineer')
- List the Books(sort)
- Question[SQL]: How can I list all book with prices greather than the average price of books of the same type?
- zoj 2727 List the Book
- ZOJ 1610 Count the Colors(线段树——区间更新)(成段染色)
- Adding new items to the Checklist
- ZOJ 2083 Win the Game
- ZOJ 3223 Journey to the Center of the Earth DP+dijstra 二维最短路
- ZOJ 3228 Searching the String(AC自动机)
- Swift Standard Library: Documented and undocumented built-in functions in the Swift standard library – the complete list with all 74 functions
- ZOJ1789(The Suspects)j简单并查集
- ZOJ 1610 Count the Colors【线段树】