[acm]HDOJ 2093 考试排名

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2093

简单字符串分析

 

/*

*g++4.8 15MS

*/

#include<iostream>

#include<algorithm>

#include<cstdio>

#include<cstring>

using namespace std;

typedef struct{

    char name[11];

    int number;

    int score;

}Student;

bool cmp(Student a,Student b) //定义比较函数

{

    if(a.number > b.number)

        return 1;

    else if(a.number == b.number)

        if(a.score < b.score)

            return 1;

    return 0;

}



Student Stu[1000];

int n,m;

int main()

{

//    freopen("input.txt", "r", stdin);

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

    int count = 0; 

    char temp[8];

    while(scanf("%s", Stu[count].name) != EOF)

    {

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

        {

            scanf("%s", temp);

            if(temp[0]=='0' || temp[0]=='-')

                continue;

            Stu[count].number++;

            int j;

            for(j=0; j<strlen(temp); ++j)

                if(temp[j]=='(')  //找到括号

                    break;

            int sum=0;

            for(int k=0; k<j; ++k)  //统计括号前的数据

            {

                sum *= 10;

                sum += temp[k]-'0';

            }

            Stu[count].score += sum;

            sum = 0;

            for(int k=j+1; k<strlen(temp)-1; ++k)  //统计括号中的数据

            {

                sum *= 10;

                sum += temp[k]-'0';

            }

            Stu[count].score += sum*m;

        }

        count++;

    }

    sort(Stu, Stu+count, cmp); 
     for(int i=0; i<count; ++i)

        printf("%-10s %2d %4d\n", Stu[i].name, Stu[i].number, Stu[i].score);

    return 0;

}

你可能感兴趣的:(ACM)