HDU-3293-sort

sort

Time Limit : 3000/1000ms (Java/Other)   Memory Limit : 65535/32768K (Java/Other)
Total Submission(s) : 8   Accepted Submission(s) : 3

Font: Times New Roman | Verdana | Georgia

Font Size:  

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

  题意就是一个排序分三个步骤: 第一按武器出发点 字典序 排序 ,第二,同一出发点按威力递减排序,第三,前面均相同的按名字字典序排序。

其中第二个排序须注意,我用的是在"goog” 前加上 't' ,组成"tgood",再用字典序排序。

代码如下:

#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#include<time.h>

struct weapon
{
    char name[25];
    char origin[25];
    char level[25];
} W[505];

//  wonderful
//  tgood
//  so-so 
 

int cmp(const void *a ,const void *b)
{
    weapon * x=(weapon * )a,*y=(weapon *) b; 
    if(strcmp(x->origin,y->origin)) 
        return strcmp(x->origin,y->origin);
    else
    {  
        int flag1=1,flag2=1;
        if(x->level[1]=='g')
        {
            x->level[0]='t';
            flag1=0;
        }
        if(y->level[1]=='g')
        {
            y->level[0]='t';
            flag2=0;
        }
        if(y->level[flag2] - x->level[flag1])
            return y->level[flag2] - x->level[flag1];
        else
            return strcmp(x->name,y->name);
    }
}

int main()
{
    int N,cnt=0;
    while(~scanf("%d",&N))  
    { 
        for(int i=0;i<N;++i) 
            scanf("%s%s%s",W[i].name,W[i].origin,W[i].level+1); 
        qsort(W,N,sizeof(W[0]),cmp);
        printf("Case %d\n",++cnt);
        if(N!=0)
        {
            printf("%s:\n",W[0].origin);
            printf("          %s %s\n",W[0].name,W[0].level+1);
            for(int i=1;i<N;++i)
            {
                if(!strcmp(W[i].origin,W[i-1].origin))
                    printf("          %s %s\n",W[i].name,W[i].level+1);
                else
                {
                    printf("%s:\n",W[i].origin);
                    printf("          %s %s\n",W[i].name,W[i].level+1);
                }
            }
        }
    }
    return 0;
}

  小白是用hash的思想做的 即 hash['w']=3,  hash['g']=2,  hash['s']=1;  比较好的思想哦。。。。。

posted @ 2011-05-16 22:52  沐阳  阅读(331)  评论(0编辑  收藏  举报