zoukankan      html  css  js  c++  java
  • List the Books

    描述

    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.

    输入

    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.

    输出

    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.

     样例输入

    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

    一道简单的根据书的名称,年份,价格关键字来排序的题目,刚开始没注意格式,两个样例之间要输出一个空白行

    #include <iostream>
    #include <stdio.h>
    #include <string.h>
    #include <algorithm>
    #include <string>
    using namespace std;
    typedef struct node
    {
        string  name;
        int year,price;
    }node;
    bool cmp(node a,node b)
    {
        if(a.name!=b.name) return a.name<b.name;
        else 
        {
            if(a.year!=b.year) return a.year<b.year;
            else return a.price<b.price;
        }
    }
    bool cma(node a,node b)
    {
        if(a.year!=b.year) return a.year<b.year;
        else 
        {
            if(a.name!=b.name) return a.name<b.name;
            else return a.price<b.price;
        }
    }
    bool cmb(node a,node b)
    {
        if(a.price!=b.price) return a.price<b.price;
        else 
        {
            if(a.name!=b.name) return a.name<b.name;
            else 
            {
                return a.year<b.year;
            }
        }
    }
    int main()
    {
        int n,m,j,k,i,s,h;
        node  t[101];
        string ss;k=0;
        while(scanf("%d",&n),n)
        {
            if(k!=0) printf("
    ");
            for(i=0;i<n;i++)
            {
                cin>>t[i].name >>t[i].year >>t[i].price;
            }
            cin>>ss;
            if(ss[0]=='N')
                sort(t,t+n,cmp);
            else if(ss[0]=='Y')
                sort(t,t+n,cma);
            else 
                sort(t,t+n,cmb);
            k++;
            for(j=0;j<n;j++)
            {
                cout<<t[j].name<<" "<<t[j].year<<" "<<t[j].price<<endl;
            }    
        }
    }
  • 相关阅读:
    波特率原理【转】
    求助大神!怎样批量删除数据库表中某个字段中同样的一段字符!
    1033. To Fill or Not to Fill (25)
    http协议
    【数据结构与算法】二叉树深度遍历(递归)
    2015届求职经历
    Codeforces Round #245 (Div. 1)——Working out
    泛泰A900 刷4.4专用中文TWRP2.7.1.1版 支持自己主动识别手机版本号(全球首创)
    实现简答LinkedList
    oracle的内存管理(之中的一个)
  • 原文地址:https://www.cnblogs.com/andrew3/p/8999997.html
Copyright © 2011-2022 走看看