• HDU3293sort


    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;  比较好的思想哦。。。。。

  • 相关阅读:
    【转】C#使用PrintDocument打印 多页 打印预览
    【转】线程间操作无效: 从不是创建控件“textBox2” 的线程访问它。
    C# 定时执行方法: System.Timers.Timer用法示例
    SQL查找数据库中所有没有主键的数据表脚本
    linux之shell编程基本语法
    Linux之shell编程条件判断-if,while,for,case
    Linux之shell编程函数使用
    2.sparkSQL--DataFrames与RDDs的相互转换
    Spark2.1集群安装(standalone模式)
    storm1.0节点间消息传递过久分析及调优
  • 原文地址:https://www.cnblogs.com/Lyush/p/2048216.html
Copyright © 2020-2023  润新知