hdu 2093 考试排名

http://acm.hdu.edu.cn/showproblem.php?pid=2093

这道题目昨天做到今天啦,总算ac了,这道题目其实不难,只是有点麻烦,先按做的数目排序,然后再按和从小到大排序,最后还要按字典序排序,最后还有格式的控制,其实处理括号的时可以用sscanf(字符串首地址(不用引号),"%d(%d)",&a,&b),它返回的是输入数据的个数,其实sscanf与scanf的区别就是前者是已经输入的字符串中读入,后者是从键盘输入,返回的都是输入的个数

下面是没有用sscanf的代码:

#include <stdio.h>

#include <string.h>

#include <stdlib.h>

#include <math.h>

struct  edg

{

        char s[100];

        int sum,ac,flag;

}e[10000];

int cmp(const void *a,const void *b)

{

     if(((struct edg *)a)->flag!=((struct edg *)b)->flag)

     return ((struct edg *)b)->flag-((struct edg *)a)->flag;

     else 

     {

                if(((struct edg *)a)->sum!=((struct edg *)b)->sum)

                return ((struct edg *)a)->sum-((struct edg *)b)->sum;

                else

                return strcmp(((struct edg *)a)->s,((struct edg *)b)->s);

     } 

    

}

int main()

{

    int n,m,i=0;

    char t[100];

    scanf("%d%d",&n,&m);

    while(scanf("%s",e[i].s)!=EOF)

    {

            int a,b;

            e[i].sum=e[i].ac=e[i].flag=0;

            for(int j=0;j<n;++j)

            {

                    int max=0;

                    a=0;b=0;

                    scanf("%s",t);

                    int c=strlen(t);

                    if(t[0]=='-'||!(t[0]-'0'))  continue;

                    else 

                    {

                         e[i].flag++;

                         if(t[c-1]==')')

                         {

                                 int k=0;

                                 while(t[k]!='(')

                                 {

                                        a=a*10+(t[k]-'0');

                                        k++;

                                 }

                                  k++;

                                  while(t[k]!=')')

                                  {

                                         b=b*10+(t[k]-'0');

                                         k++;

                                  }

                                  e[i].sum+=a;

                                  e[i].ac+=b; 

                         }

                         else

                         e[i].sum+=atoi(t);   

                    }

            }

            e[i].sum+=e[i].ac*m; i++; 

    }   

    qsort(e,i,sizeof(e[0]),cmp);

    for(int  j=0;j<i;++j)

    printf("%-10s %2d %4d\n",e[j].s,e[j].flag,e[j].sum);

   // system("pause");

    return 0;

}

原文地址:https://www.cnblogs.com/yuelingzhi/p/2130729.html