您的位置:首页 > 其它

HDU--杭电--3293--sort--结构体排序

2013-07-30 23:53 453 查看

sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)

Total Submission(s): 596    Accepted Submission(s): 274


[align=left]Problem Description[/align]
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.



 

 

[align=left]Input[/align]
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.
 

 

[align=left]Output[/align]
Please output your list after sorting (format according to sample, pay attention to the spaces,ten spaces need ^ ^).
 

 

[align=left]Sample Input[/align]

5
knife qizhou so-so
gun qizhou wonderful
knife zhengzhou good
stick zhengzhou good
rope shengzhou so-so

 

 

[align=left]Sample Output[/align]

Case 1
qizhou:
gun wonderful
knife so-so
shengzhou:
rope so-so
zhengzhou:
knife good
stick good

 

#include <iostream>

#include <cstring>

#include <algorithm>
using namespace std;

struct
ssss

{

    char
mingzi[22],chandi[22],pinzhi[22]; 
//名字,产地,品质

}
ss[555];

int
good(const char *a,const
char *
b)  //做一个类似于strcmp的比较函数,按品质分别返回-1,0,1

{

    if(!
strcmp(a,"wonderful"))

        if(!
strcmp(b,"wonderful"))return
0;

        else return
1;

    if(!
strcmp(a,"good"))

        if(!
strcmp(b,"wonderful"))return
-
1;

        else if(!
strcmp(b,"good"))return
0;

        else return
1;

    if(!
strcmp(b,"so-so"))return
0;

    return -
1;

}

bool
cmp(const ssss &a,const
ssss &b)

{

    if(
strcmp(a.chandi,b.chandi)<0)return
1;

    if(!
strcmp(a.chandi,b.chandi))

    if(
good(a.pinzhi,b.pinzhi)>0||good(a.pinzhi,b.pinzhi)==0&&strcmp(a.mingzi,b.mingzi)<0)

        return
1;

    return
0;

}

int main (void)

{

    int
n,i,j,k,l=1;

    char
s[22];

    while(
cin>>n)

    {

        for(
i=0;i<n;i++)

            cin>>ss[i].mingzi>>ss[i].chandi>>ss[i].pinzhi;

        sort(ss,ss+n,cmp);

        cout<<"Case "<<l++<<endl;

        for(
i=0;i<n;i++)

        {

            if(
strcmp(s,ss[i].chandi))strcpy(s,ss[i].chandi),cout<<s<<":"<<endl
//如果产地和s记录的不同,s换产地并输出产地


            cout<<"          "<<ss[i].mingzi<<"
"<<ss[i].pinzhi<<endl;

        }

    }

    return
0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: