15.List the Books——列出书籍

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

中文翻译:

描述

吉姆喜欢读书,他有很多书,有时他很难管理。所以他要求你的帮助来解决这个问题。根据分类标准,吉姆只想对书籍的名称,印刷年份和价格感兴趣,希望得到他的书籍的分类清单。

输入

问题包括多个测试用例。在每个测试用例的第一行中,有一个整数n,它指定了Jim拥有的书籍数量。n将是一个小于100的正整数。接下来的n行以Name Year Price格式给出书籍的信息。名称将是一个字符串,由字母表中的最多80个字符组成,Year和Price将为正整数。然后是排序标准,可以是姓名,年份或价格。您的任务是根据非上升顺序的排序标准按升序发出图书清单。注意:该名称是第一个标准,年份是第二个,价格是第三个。这意味着如果排序标准是年份并且您有两本同一年份的书籍,您可以根据他们的名称对它们进行排序。如果他们再次等于他们的价格。在这三个参数中,没有两本书是相同的。输入将由n = 0的情况终止。

产量

对于每个测试用例,输出书籍列表,每本书都在一行中。在每行中,您应该以Name Year Price格式输出,这三个参数应该只分隔一个空格。您应该在两个测试用例之间输出一个空行。

样本输入

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

样本输出

TheC ++ StandardLibrary 2002 108 
LearningGNUEmacs 2003 68 
ArtificialIntelligence 2005 75 

GhostStory 2001 1 
WuXiaStory 2000 2 
WeekEnd 1998 5 
SFStory 1999 10

对于这道题,题目中描述了一本书包含了三个信息,首先想到的是利用结构体来进行存储,这样方便在接下来进行排序,整个题很简单,但是代码比较繁琐,尤其是在按照题目要求的三种排序方式上的处理需要写三个sort来进行操作。

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<iostream>
#include<algorithm>
using namespace std;
struct B
{
    string a;
    int year;
    int price;
} b[101];
bool cmp1(B a,B b)
{
       if(a.a!=b.a)
        return a.a<b.a;
    else if(a.year!=b.year)
        return a.year<b.year;
    else
        return a.price<b.price;

}
bool cmp2(B a,B b)
{
     if(a.year!=b.year)
        return a.year<b.year;
    else if(a.a!=b.a)
        return a.a<b.a;
    return a.price<b.price;

}
bool cmp3(B a,B b)
{
    if(a.price!=b.price)
        return a.price<b.price;
    else if(a.a!=b.a)
        return a.a<b.a;
    return a.year<b.year;

}
int main()
{
    int i,j,k=1,n;
    string c;
    while(scanf("%d",&n)!=EOF)
    {
        if(n==0)
            break;
        for(i=0; i<=n-1; i++)
            cin>>b[i].a>>b[i].year>>b[i].price;
        cin>>c;
        if(c=="Name")
            {
                sort(b,b+n,cmp1);
            }
        if(c=="Year")
            {
                sort(b,b+n,cmp2);
            }
        if(c=="Price")
            {
                sort(b,b+n,cmp3);
            }
        if(k)
            k=0;
        else
            cout<<endl;
        for(i=0; i<=n-1; i++)
            cout<<b[i].a<<" "<<b[i].year<<" "<<b[i].price<<endl;
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值