杭电OJ2093 有详细注解

思路
题目要求输入为总共的题数和罚分数
输入次数为一直循环输入每个人的名字和每道题的分数
用一个结构体输出每个人的成绩排名
成绩排名需要写一个判断函数用于排序,这里可以参考C++排序函数的用法

#include<string>
#include<iostream>  //cin输入的库函数
#include<algorithm>  //sort函数库函数
#include<stdio.h>
using namespace std;
struct student
{
    string name;
    int num;
    int delscore;
};
bool cmp(student a,student b)
{
    return a.num!=b.num? a.num>b.num:a.delscore<b.delscore;
}
//定义一个输出的结构体
int main()
{
    int n,m,d,fail,time; //n为考试题数,m为每道题的罚分,fail为提交错误的次数,time为提交正确的耗时
    string str;
    student stu[1000];
    cin>>n>>m;
    d=0;
    while(cin>>stu[d].name) //因为没有规定输入行数,所以用while循环输入name可以保证循环不会结束
    {
        stu[d].num=0;        //清洗数据
        stu[d].delscore=0;
        for(int i=0;i<n;i++)  //遍历输入n道题
        {
            fail=0;
            time=0;
            cin>>str;   //输入每道题的数据
            if(str[0]=='0'||str[0]=='-') //首字母为0或者-不得分
                continue;   //跳出该层循环(i++)
            for(int j=0;j<str.length();j++)  //遍历str
            {
                if(str[j]=='(')  //当遇到(时说明通过但有错误题目
                {
                   j++;    //j指向str的下一个字符,找到错误输入的次数
                while(str[j]!=')')  //遍历str知道遇到)
                {
                    fail=fail*10+str[j]-'0';  //模版,遍历过程中存储字符串中数字的大小
                    j++; 
                }
                break; //break跳出while循环
                }
                else  //否则不含(  这里包括一次正确和有错误的AC,都可以通过else输出ac所花的时间
                {
                    time=time*10+str[j]-'0';  //输出
                }
            }
            stu[d].num++;   
            stu[d].delscore+=m*fail+time;
        }
        d++;
    }
    sort(stu,stu+d,cmp);  //sort函数可以对结构体按cmp函数进行排序
    for(int i=0;i<d;i++) //按照题目的要求进行输出
        printf("%-10s %2d %4d\n", stu[i].name.c_str(),stu[i].num, stu[i].delscore);
    return 0;
}
发布了28 篇原创文章 · 获赞 7 · 访问量 1181

猜你喜欢

转载自blog.csdn.net/weixin_44433678/article/details/97047404