zoukankan      html  css  js  c++  java
  • HDU1862EXCEL排序

    其实最近都没有兴趣做排序题目,因为我觉得纯粹排序对我而言进步不大,但是舍友TLE了,叫我试一试。

    整道题的思路很简单啦,我用的是快排,比较的原则也给得很清楚,不必多言,我没有用stdlib的快排,也没有用scanf,printf等IO,因为我觉得没什么必要啦。

    当然,如果用了程序当然会更快~

    我甚至有点觉得这题目是模拟题……CE一次,没包括string头文件,之后就AC了。

    /*******************************************************************************/
    /* OS           : 3.2.0-58-generic #88-Ubuntu SMP Tue Dec 3 UTC 2013 GNU/Linux
     * Compiler     : g++ (GCC)  4.6.3 (Ubuntu/Linaro 4.6.3-1ubuntu5)
     * Encoding     : UTF8
     * Date         : 2014-03-26
     * All Rights Reserved by yaolong.
    *****************************************************************************/
    /* Description: ***************************************************************
    *****************************************************************************/
    /* Analysis: ******************************************************************
    *****************************************************************************/
    /*****************************************************************************/
    
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<string>
    using namespace std;
    
    class student{
        public:
        string id ;
        string name;
        int score;
        student(string i,string n,int s){
          id=i;
          name=n;
          score=s;
        }
        student (){}
        student & operator=(const student& tmp){
         this->id=tmp.id;
           this->name=tmp.name;
         this->score=tmp.score;
          return *this;
        }
        void show(){
           cout<<id<<" "<<name<<" "<<score<<endl;
        }
    };
    student stu[100001];
    bool isBig1(student &s1,student &s2){
        for(int i=0;i<6;i++)
           if(s1.id[i]>s2.id[i]) return 0;
           else if(s1.id[i]<s2.id[i]) return 1;
    
    
       return 0;
    }
    bool isBig2(student &s1,student &s2){
        int len1=s1.name.length();
        int len2=s2.name.length();
        int min=len1>len2?len2:len1;
        if(s1.name==s2.name){
           return isBig1(s1,s2);
        }
    
        for(int i=0;i<min;i++){
            if(s1.name[i]>s2.name[i]) return 0;
            else if(s1.name[i]<s2.name[i]) return 1;
        }
        if(len1>len2){
           return 0;
        }else if(len1<len2){
           return 1;
        }
    
       return 0;
    }
    bool isBig3(student &s1,student &s2){
    
       if(s1.score==s2.score){
         return isBig1(s1,s2);
       }
       return s1.score<s2.score;
    }
    int partition(student a[],int p,int r,int cases)
    {
       student x=a[r];//通常,拿最后一个值,作为预期的中间值
        int middle=p;//记录“较小的一段数据”的最大下标。通常这个值在p和r的中间,故起名middle
        for(int j=p; j<r; j++)
        {
            switch(cases){
               case 1:   //按id排序
                if(isBig1(a[j],x))
                {
                    if(j!=middle)
                    {
                    student temp=a[middle];
                    a[middle]=a[j];
                    a[j]=temp;
                    }
                      middle++;
                   }
    
               break;
               case 2:
                if(isBig2(a[j],x))
                {
                    if(j!=middle)
                    {
                    student temp=a[middle];
                    a[middle]=a[j];
                    a[j]=temp;
                    }
                      middle++;
                   }
               break;
               case 3:
                if(isBig3(a[j],x))
                {
                    if(j!=middle)
                    {
                    student temp=a[middle];
                    a[middle]=a[j];
                    a[j]=temp;
                    }
                      middle++;
                   }
               break;
    
    
            }
    
        }
        student temp=a[r];
        a[r]=a[middle];
        a[middle]=temp;
        return middle;
    }
    
    
    void QuickSort(student a[],int p,int r,int c)
    {
        if(p<r)
        {
            int q=partition(a,p,r,c);
            QuickSort(a,p,q-1,c);
            QuickSort(a,q+1,r,c);
        }
    }
    int  main()
    {
        #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        #endif
       int n,c,i;
       int p=0;
       while(cin>>n>>c&&(n||c)){
           p++;
           for(i=0;i<n;i++){
              cin>>stu[i].id>>stu[i].name>>stu[i].score;
           }
    
    
           cout<<"Case "<<p<<":
    ";
           QuickSort(stu,0,n-1,c);
           for(i=0;i<n;i++)
           stu[i].show();
    
    
    
    
    
       }
    
    
        #ifndef  ONLINE_JUDGE
        fclose(stdin);
        #endif
    
        return 0;
    
    }
    
    
    

     

  • 相关阅读:
    GDC快讯,腾讯CMatrix布局云游戏B端领域
    如何测试小程序? 腾讯智慧零售保障优衣库小程序体验优化
    一分钟读懂兼容报告:测试过程视频复现,问题定位很轻松
    一到秒杀就瘫痪?压测大师保你后台稳健
    how2j-springcloud-摘抄
    问题1
    springcloud---how2java--记录零碎的信息
    how2java-springcloud-demo
    oracle 查两个日期之间数据有多少条
    非官网渠道下单导致的错误
  • 原文地址:https://www.cnblogs.com/dengyaolong/p/3697210.html
Copyright © 2011-2022 走看看