Problem Description
As is known to all, long long ago sailormoon once was an association of fighters. Till now, sailormoon is also an association of girls. Owe to some unknown reasons, girls are necessary to fight for peace.
Their boss, lcy, wants to strengthen their ability, so he give them his precious collections—weapons for many years. Because these collections are really age-old, it is hard to recognize from one to another. So girls intend to sort them before they use. Each weapon has its name, origin and level of harmfulness ( level contains three ranks: wonderful, good, so-so).
In order to make it clear, girls want to sort like this:
firstly,sort according to the origin (sort by lexicographic order), if two or more have the same origin, they will be sorted together;
secondly, sort according ranks, wonderful is the best, good is next, the third is so-so;
thirdly, if two or more have same origin and rank, sort them according to the lexicographic order.
Input
Input contains multiply cases. Each case contains several lines. First line is an integer N(0<N<=500), representing the number of weapons. Then N lines follows. Each line represent a kind of weapon, and contains a set of strings representing name, origin and level of harmfulness.
Each string will not exceed 20 characters.
Sure that same origin will not exist the same weapon.
Output
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
Sample Input
5 knife qizhou so-so gun qizhou wonderful knife zhengzhou good stick zhengzhou good rope shengzhou so-so
Sample Output
Case 1 qizhou: gun wonderful knife so-so shengzhou: rope so-so zhengzhou: knife good stick good

题目很清晰,先按来源地排序,然后排武器,最后是武器威力。就是排序算法!

直接上代码:

#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
struct sa
{
    char a[25];
    char b[25];
    int c;
}str[550];
char k[3][20]={"so-so","good","wonderful"};
int l=1;
int cmp(const sa &p,const sa &q)
{
    if(strcmp(p.b,q.b)!=0)
    return strcmp(p.b,q.b)<0;
    else if(p.c!=q.c)
    return p.c>q.c;
    else
    return strcmp(p.a,q.a)<0;
}
int main()
{
    int n;
    while(cin>>n)
    {
        char tmp[25];
        for(int i=0;i<n;i++)
        {
            cin>>str[i].a>>str[i].b;
            cin>>tmp;
            if(strcmp(tmp,"so-so")==0)
            str[i].c=0;
            if(strcmp(tmp,"good")==0)
            str[i].c=1;
            if(strcmp(tmp,"wonderful")==0)
            str[i].c=2;
        }
        sort(str,str+n,cmp);
        //for(int i=0;i<n;i++)
        //cout<<str[i].a<<" "<<str[i].b<<" "<<str[i].c<<endl;
        cout<<"Case "<<l++<<endl;
        cout<<str[0].b<<":"<<endl;
        cout<<"          "<<str[0].a<<" "<<k[str[0].c]<<endl;
        for(int i=1;i<n;i++)
        {
           if(strcmp(str[i].b,str[i-1].b)==0)
           cout<<"          "<<str[i].a<<" "<<k[str[i].c]<<endl;
           else
           {
               cout<<str[i].b<<":"<<endl;
               cout<<"          "<<str[i].a<<" "<<k[str[i].c]<<endl;
           }
        }
    }return 0;
}
0
Posted in ACM

Leave a Comment:

电子邮件地址不会被公开。