hdu 3293 sort

hdu 3293 sort

题目:

sort

Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)
Total Submission(s): 752    Accepted Submission(s): 355


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.

hdu 3293 sort_第1张图片
 

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

 

题意:

简单的排序题,输出有些麻烦,可以用哈希做,效果一样,排序算法可以调用系统库函数,也可以自己手写。
按照来源(origin)、等级(rank)、武器(weapons)的先后顺序排序,然后按要求输出。

代码:

#include<stdio.h>
#include<string.h>

char smap[3][20]= {"wonderful","good","so-so"};
struct node
{
    char origin[51];
    char weapons[51];
    int ranks;
};
int to_int(char *p)
{
    int i;
    for(i=0; i<3; i++)
        if(strcmp(p,smap[i])==0)
            break;
    return i;
}
void mysort(node * wo,int len)
{
    int i,k,j;
    node temp;
    for(i=0; i<len; i++)
    {
        k=i;
        for(j=i; j<len; j++)
        {
            if(strcmp(wo[k].origin,wo[j].origin)==0)
            {
                if(wo[k].ranks==wo[j].ranks)
                {
                    if(strcmp(wo[k].weapons,wo[j].weapons)>0) k=j;
                }
                else if(wo[k].ranks>wo[j].ranks)  k=j;
            }
            else if(strcmp(wo[k].origin,wo[j].origin)>0) k=j;
        }
        if(k!=i)
        {
            temp=wo[i];
            wo[i]=wo[k];
            wo[k]=temp;
        }
    }

}
int main()
{
    int n,flag,i,tt=1;
    struct node wo[501];
    char s[51];
    while(scanf("%d\n",&n)!=EOF)
    {
        for(i=0; i<n; i++)
        {
            scanf("%s %s %s",wo[i].weapons,wo[i].origin,s);
            wo[i].ranks=to_int(s);
        }
        mysort(wo,n);
        strcpy(s,wo[0].origin);
        flag=1;
        printf("Case %d\n",tt++);
        for(i=0;i<n;i++)
        {
            if(flag==1)
            {
                printf("%s:\n",s);
                flag=0;
            }
            printf("          %s %s\n",wo[i].weapons,smap[wo[i].ranks]);
           if(strcmp(s,wo[i+1].origin))
            {
                strcpy(s,wo[i+1].origin);
                flag=1;
            }
        }
    }
    return 0;
}






你可能感兴趣的:(HDU,3293)