List the Books

List the Books

时间: 1ms        内存:64M

描述:

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

提示:

参考答案(内存最优[752]):

#include <stdio.h>
int main()
{
    char a[100][100]= {0},d[100],f[10];
    int b[100]= {0},c[100]= {0},i,j,n,e;
    while(scanf("%d",&n)&&n)
    {
        for(i=0; i<n; i++)
            scanf("%s%d%d",a[i],&b[i],&c[i]);
        scanf("%s",f);
        if (f[0]=='N')
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                {
                    if (strcmp(a[i],a[j])>0)
                    {
                        strcpy(d,a[i]);
                        strcpy(a[i],a[j]);
                        strcpy(a[j],d);
                        e=b[i];
                        b[i]=b[j];
                        b[j]=e;
                        e=c[i];
                        c[i]=c[j];
                        c[j]=e;
                    }
                    if (strcmp(a[i],a[j])==0)
                    {
                        if (b[i]>b[j])
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                        else if (c[i]>c[j])
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                    }
                }
        if (f[0]=='Y')
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                {
                    if (b[i]>b[j])
                    {
                        strcpy(d,a[i]);
                        strcpy(a[i],a[j]);
                        strcpy(a[j],d);
                        e=b[i];
                        b[i]=b[j];
                        b[j]=e;
                        e=c[i];
                        c[i]=c[j];
                        c[j]=e;
                    }
                    if (b[i]==b[j])
                    {
                        if (strcmp(a[i],a[j])>0)
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                        else if (c[i]>c[j])
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                    }
                }
        if (f[0]=='P')
            for(i=0; i<n; i++)
                for(j=i+1; j<n; j++)
                {
                    if (c[i]>c[j])
                    {
                        strcpy(d,a[i]);
                        strcpy(a[i],a[j]);
                        strcpy(a[j],d);
                        e=b[i];
                        b[i]=b[j];
                        b[j]=e;
                        e=c[i];
                        c[i]=c[j];
                        c[j]=e;
                    }
                    if (c[i]==c[j])
                    {
                        if (strcmp(a[i],a[j])>0)
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                        else if (b[i]>b[j])
                        {
                            strcpy(d,a[i]);
                            strcpy(a[i],a[j]);
                            strcpy(a[j],d);
                            e=b[i];
                            b[i]=b[j];
                            b[j]=e;
                            e=c[i];
                            c[i]=c[j];
                            c[j]=e;
                        }
                    }
                }
        for(i=0; i<n; i++)
            printf("%s %d %d\n",a[i],b[i],c[i]);
        printf("\n");
    }
    return 0;
}

参考答案(时间最优[0]):

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;
struct book
{
    string name;
    int year;
    int price;
} b[1001];
bool cmp_year(book a,book b)
{
    if(a.year!=b.year)return a.year<b.year;
    if(a.name!=b.name)return a.name<b.name;
    return a.price<b.price;
}
bool cmp_name(book a,book b)
{
    if(a.name!=b.name)return a.name<b.name;
    if(a.year!=b.year)return a.year<b.year;
    return a.price<b.price;
}
bool cmp_price(book a,book b)
{
    if(a.price!=b.price)return a.price<b.price;
    if(a.name!=b.name)return a.name<b.name;
    return a.year<b.year;
}
int main()
{
    bool first=true;
    int n;
    while(cin>>n&&n)
    {
        if(!first)cout<<endl;
        else first=false;
        int i;
        for(i=0; i<n; i++)
        {
            cin>>b[i].name>>b[i].year>>b[i].price;
        }
        string y="Year";
        string p="Price";
        string criteria;
        cin>>criteria;
        if(criteria==y)
        {
            sort(b,b+n,cmp_year);
        }
        else if(criteria==p)
        {
            sort(b,b+n,cmp_price);
        }
        else
        {
            sort(b,b+n,cmp_name);
        }
        for(i=0; i<n; i++)
            cout<<b[i].name<<" "<<b[i].year<<" "<<b[i].price<<endl;
    }
    return 0;
}

题目和答案均来自于互联网,仅供参考,如有问题请联系管理员修改或删除。

点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注