P1165 List the Books
Time Limit: 1 Sec Memory Limit: 64 MB Submit: 455 Solved: 235
[Submit][Status][Forum]
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
*水题,掌握sort函数即可AC。根据题意实际上是要求我们根据三个条件进行排序。*值得注意的是sort不止可以对实数数组排序,还可以对字符数组排序。
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;
struct book//结构体,书名/时间/价格
{
char a[100];
int b;
int c;
};
bool cmp1(struct book a,struct book b)//此处用(且必须用)a和b代指所用的数组元素,注意a和b前面要有类型
{
if(strcmp(a.a,b.a)!=0)
return a.a<b.a;
else if(a.b!=b.b)
return a.b<b.b;
else return a.c<b.c;
}
bool cmp2(struct book a,struct book b)
{
if(a.b!=b.b)
return a.b<b.b;
else if(strcmp(a.a,b.a)!=0)
return a.a<b.a;
else return a.c<b.c;
}
bool cmp3(struct book a,struct book b)
{
if(a.c!=b.c)
return a.c<b.c;
else if(strcmp(a.a,b.a)!=0)
return a.a<b.a;
else return a.b<b.b;
}
int main()
{
struct book list[105];
int i,k=0,n;
char d[10];
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
{
scanf("%s%d%d",list[i].a,&list[i].b,&list[i].c);
}
scanf("%s",d);
if(strcmp(d,"Name")==0)
sort(list,list+n,cmp1);//直接传递数组名,不用且不能加类型名
if(strcmp(d,"Year")==0)
sort(list,list+n,cmp2);
if(strcmp(d,"Price")==0)
sort(list,list+n,cmp3);
if(k==0)//使得每两个样例之间有个空白行
k=1;
else printf("\n");
for(i=0;i<n;i++)
printf("%s %d %d\n",list[i].a,list[i].b,list[i].c);
}
return 0;
}