现在的位置: 首页 > 综合 > 正文

hdu3293(运用结构体)

2018年02月22日 ⁄ 综合 ⁄ 共 2363字 ⁄ 字号 评论关闭
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<stdio.h>
#include<string.h>
struct u
{
    char name[30],addr[30],nice[30];
    int d;
};
struct u a[100000],temp;
int main()
{

    int t,i,j,lenmax,k=0;
    while(scanf("%d",&t)==1)
    {
        lenmax=0;
        for(i=0;i<t;i++)
        {
            getchar();
            scanf("%s %s %s",a[i].name,a[i].addr,a[i].nice);

            if(strlen(a[i].addr)>lenmax)
            lenmax=strlen(a[i].addr);

            if(strcmp(a[i].nice,"wonderful")==0)
            a[i].d=3;
           else if(strcmp(a[i].nice,"good")==0)
            a[i].d=2;
           else if(strcmp(a[i].nice,"so-so")==0)
            a[i].d=1;
        }
        for(j=0;j<t;j++)
        for(i=j+1;i<t;i++)
        if(strcmp(a[j].addr,a[i].addr)>0)
        {
            temp=a[j];a[j]=a[i];a[i]=temp;
        }
        else if(strcmp(a[j].addr,a[i].addr)==0)
        {
            if(a[j].d<a[i].d)
            {
                temp=a[j];a[j]=a[i];a[i]=temp;
            }
            else if(a[j].d==a[i].d)
            if(strcmp(a[j].name,a[i].name)>0)
            {
                temp=a[j];a[j]=a[i];a[i]=temp;
            }
        }

        printf("Case %d\n",++k);
        int flog=1,e;
        
        for(i=0,j=0;i<t;i++)
        if(strcmp(a[i].addr,a[j].addr)==0)
        {
            if(flog)
            printf("%s:\n",a[j].addr);
            flog=0;
            
            printf("          ");
        printf("%s %s\n",a[i].name,a[i].nice);
        }
        else
        {
            j=i;i--;flog=1;
        }
    }
}

【上篇】
【下篇】

抱歉!评论已关闭.