浏览 1833 次
锁定老帖子 主题:003排名
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (7)
作者 正文
   发表时间:2009-04-13   最后修改:2009-04-13
C++
首先一个正整数t,表示测试用例的数量。
对每组数据,首先输入一个n(1<=n<=10000),表示参赛队伍的数量。然后有n行,每行为:
Team-name Problems Penalty Info
其中Team-name表示队名(长度不超过20),Problems表示解题数量(32位有符号整数可以表示),Penalty表示罚时(32位有符号整数可以表示),Info为M表示女队,N表示正常队伍。
排序后的结果,格式与输入相同。
首先根据题目数量由多至少,同等题目情况下女队优先,然后罚时少者优先,最后按队名字典序。
每组样例后输出一个空行。
   发表时间:2009-04-15  
简单的赛程排名:
#include <iostream>
using namespace std;
#define ARRAYLENGTH 9
#define GROUPLENGTH 10000
#include <math.h>
#include <ctype.h>
#include <vector>
#include <algorithm>
char* splitDigit(string digitStr);
//char* splitInt(int a);
bool isDigit(string testCaseCout);
int get_Int(char c);
int stringToInt(string teamName);
bool isLetter(string str);
//int calculate(int number);
char get_Char(int c);
bool checkDigit(char c);
bool checkNORM(char c);
bool compareString(string str1,string str2);

/*
数据的复杂度,此程序目前测试用例为1个 
首先根据题目数量由多至少,同等题目情况下女队优先,然后罚时少者优先,最后按队名字典序。
*/ 
/*
字典排序 
*/
char *c = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
char number[ARRAYLENGTH] = {'1','2','3','4','5','6','7','8','9'};

struct paiming
{
       string teamName;
       int problems;
       int penalty;
       char info;       
};
bool comparator(struct paiming p1,struct paiming p2);
void byLetter(vector<struct paiming> v);

/*按解题数排序*/
bool proSort(struct paiming p1,struct paiming p2)
{
     int t1 = p1.problems;
     int t2 = p2.problems;
     if(t1 != t2)
      {
           return t1 > t2; 
      }    
     return true;
}

/*按N/M排序*/
bool infoSort(struct paiming p1,struct paiming p2)
{
     int t1 = p1.problems;
     int t2 = p2.problems;
     if(t1 == t2)
     {
         char i1 = p1.info;
         char i2 = p2.info;
         if(i1 != i2)
         {
            if(i1 == 'M')
            {
                return true;
            }else
            {
                return false;  
            }
         } 
     }else
     {
          return proSort(p1,p2);
     }
     return true;
}
/*按罚时排序*/
bool paSort(struct paiming p1,struct paiming p2)
{
     int t1 = p1.problems;
     int t2 = p2.problems;
     char c1 = p1.info;
     char c2 = p2.info;
     if(t1 == t2 && c1 ==  c2)
       {
       int i1 = p1.penalty;
       int i2 = p2.penalty;
       if(i1 != i2)
       {
          return i1 < i2;
       }
     }else
     {
        if(t1 ==  t2)
        {
            if(c1 == 'M')
            {
                return true;
            }else
            {
                return false;  
            }  
        }else
        {
            return proSort(p1,p2);
        }
        
     }
     
     return true;
}

/*字典排序*/
bool compareString(string str1,string str2)
{    
       char c1,c2;
       string ts;
       if(str1.length() >= str2.length())
       {
           ts =  str2;
       }
       else
       {
           ts = str1;
       }
       for(int i = 0;i < ts.length();i++)
       {
          if((c1 = str1[i]) != (c2 = str2[i]))
          {
               return c1 < c2;
          }
       }
     return true;
     
}
bool comparator_letter(struct paiming p1,struct paiming p2)
{
     
     int t1 = p1.problems;
     int t2 = p2.problems;
     char c1 = p1.info;
     char c2 = p2.info;
     int i1 = p1.penalty;
     int i2 = p2.penalty;
     if(t1 == t2 && c1 == c2 && i1 == i2)
     {
       string t1 = p1.teamName;
       string t2 = p2.teamName;
       return compareString(t1,t2);
     }else
     {
       if(t1 == t2)
       {
             if(c1 == c2)
             {
                return i1 < i2;  
             }else
             {
                if(c1 == 'M')
                {
                    return true;
                }else
                {
                    return false;  
                }  
             }
       }else
       {
            return proSort(p1,p2);
       }
     }
     return true;
     
}

/*------------------以上为排序------------------------------*/
/*检验是否是N或者M*/
bool checkNORM(char c)
{
     if(c == 'M' || c == 'N')
     {
          return true;
     }
     return false;
}
/*检验是否为数字*/
bool checkDigit(string a)
{
     bool flag = true;
     char *p= splitDigit(a);
     while(*p != '\0')
     {
              char pp = *p++;
              if(pp < '0' || pp > '9')
              {
                  flag = false;
                  break;
              }
     }
     return flag;
}
/*检验是否为字母*/
bool isLetter(string str)
{
   for(int i = 0;i < str.length();i++)
   {
       char c = str[i];
       char up_c = toupper(c);
       if(up_c < 'A' || up_c > 'Z')
       {
          return false; 
       }else
       {
          continue; 
       }
   }
   return true;  
}

/*判断数字*/ 
bool isDigit(string testCaseCout)
{
    bool flag = true;
    char *p = splitDigit(testCaseCout);
    while(*p != '\0')
    {
       int i = 0;
       char pp = *p++;
       while(i < ARRAYLENGTH)
       {
           if(pp == number[i])     
            {
               break;    
            }
            else
            {
               if(i == ARRAYLENGTH - 1)
               {
                   flag = false;
                   break;
               }
               i++; 
               continue; 
            }
       }
       if(!flag)
           break;
     }
     return flag;
}
/*将string化为int型*/
int stringToInt(string teamName)
{
     int sum = 0;
     char str[10000] = {'\0'};
     for(int i = 0;i < teamName.length();i++)
     {
         str[i] = teamName[i];
         int ii = get_Int(str[i]);
         double d = pow(10,teamName.length()-i-1);
         sum += (int)(ii * d);
     }
     return sum;
}
/*拆分字符串*/
char* splitDigit(string digitStr)
{
     int _len = digitStr.length();
     static char str[100] = {'\0'};
     for(int i = 0;i < _len;i++)
     {
          str[i] = digitStr[i];
     }
     return str;//不能返回栈空间即将释放的内存空间 
}
/*原先字符当作数字*/
int get_Int(char c)
{
  static int cc = 0;
  switch (c)
  {
     case '1':
          cc = 1;
           break;
     case '2':
          cc = 2;
           break;
     case '3':
          cc = 3;
           break;
     case '4':
           cc = 4;
           break;
     case '5':
          cc = 5;
           break; 
     case '6':
           cc = 6;
           break;
     case '7':
           cc = 7;
           break;
     case '8':
          cc = 8;
           break;
     case '9':
           cc = 9;
           break;
     default:
           cc = 0;
           break;      
  }
  return cc;
}
void t()
{
     string testCaseCout;
     cout<<"输入测试用例数量:"<<endl;// 
     cin>>testCaseCout;
     while(!isDigit(testCaseCout))
     {
         cout<<"测试用例数量不是数字,请重新输入:"<<endl;
         cin>>testCaseCout;
     }
     int testCaseNumber = stringToInt(testCaseCout);
     string groupCount;
     cout<<"输入参赛队伍数量:"<<endl;
     cin>>groupCount;
     while(!isDigit(testCaseCout))
     {
         cout<<"参赛队伍数量不是数字,请重新输入:"<<endl;
         cin>>groupCount;                        
     }
     int _groupCount = stringToInt(groupCount);
     while(_groupCount< 0 || _groupCount > GROUPLENGTH)
     {
         cout<<"参赛队伍数量在1~10000,请重新输入:"<<endl;
         cin>>groupCount;              
     }
     _groupCount = stringToInt(groupCount);
     struct paiming pa;
     vector<struct paiming> v;
     cout<<"请分别输入队名,解题数量,罚时,Info:"<<endl;
     string prob,penal;
     for(int i = 0;i < _groupCount;i++)
     {
         cin>>pa.teamName>>prob>>penal>>pa.info;
         while(!isLetter(pa.teamName) || !checkNORM(pa.info))
         {
              cout<<"请输入对应的类型数据:"<<endl;
              cin>>pa.teamName>>prob>>penal>>pa.info;
         }
          pa.problems = stringToInt(prob);
          pa.penalty = stringToInt(penal);
          v.push_back(pa);
     }
    sort(v.begin(),v.end(),comparator_letter);
   for(vector<struct paiming>::const_iterator iter = v.begin() ; iter < v.end() ; ++iter)
   {
     cout<<iter->teamName<<" "<<iter->problems<<" "<<iter->penalty<<" "<<iter->info<<endl;  
   }
}
int main()
{
    t();
    system("PAUSE");
}
0 请登录后投票
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics