问题描述
\hspace{17pt}
程序设计思维作业和实验使用的实时评测系统,具有及时获得成绩排名的特点,那它的功能是怎么实现的呢?
\hspace{17pt}
我们千辛万苦怼完了不忍直视的程序并提交以后,评测系统要么返回AC,要么是返回各种其他的错误,不论是怎样的错法,它总会给你记上一笔,表明你曾经在这儿被坑过,而当你历经千辛终将它AC之后,它便会和你算笔总账,表明这题共错误提交了几次。
\hspace{17pt}
在岁月的长河中,你通过的题数虽然越来越多,但通过每题时你所共花去的时间(从最开始算起,直至通过题目时的这段时间)都会被记录下来,作为你曾经奋斗的痕迹。特别的,对于你通过的题目,你曾经的关于这题的每次错误提交都会被算上一定的单位时间罚时,这样一来,你在做出的题数上,可能领先别人很多,但是在做出同样题数的人中,你可能会因为罚时过高而处于排名上的劣势。
\hspace{17pt}
例如某次考试一共八道题(A,B,C,D,E,F,G,H),每个人做的题都在对应的题号下有个数量标记,负数表示该学生在该题上有过的错误提交次数但到现在还没有AC,正数表示AC所耗的时间,如果正数a跟上了一对括号,里面有个正数b,则表示该学生AC了这道题,耗去了时间a,同时曾经错误提交了b次。例子可见下方的样例输入与输出部分。
Input
\hspace{17pt} 输入数据包含多行,第一行是共有的题数n(1≤n≤12)以及单位罚时m(10≤m≤20),之后的每行数据描述一个学生的信息,首先是学生的用户名(不多于10个字符的字串)其次是所有n道题的得分现状,其描述采用问题描述中的数量标记的格式,见上面的表格。
Output
\hspace{17pt} 根据这些学生的得分现状,输出一个实时排名。实时排名显然先按AC题数的多少排,多的在前,再按时间分的多少排,少的在前,如果凑巧前两者都相等,则按名字的字典序排,小的在前。每个学生占一行,输出名字(10个字符宽),做出的题数(2个字符宽,右对齐)和时间分(4个字符宽,右对齐)。名字、题数和时间分相互之间有一个空格。数据保证可按要求的输出格式进行输出。
Sample Input
8 20
GuGuDong 96 -3 40(3) 0 0 1 -8 0
hrz 107 67 -3 0 0 82 0 0
TT 120(3) 30 10(1) -3 0 47 21(2) -2
OMRailgun 0 -99 -8 0 -666 -10086 0 -9999996
yjq -2 37(2) 13 -1 0 113(2) 79(1) -1
Zjm 0 0 57(5) 0 0 99(3) -7 0
Sample Output
TT 5 348
yjq 4 342
GuGuDong 3 197
hrz 3 256
Zjm 2 316
OMRailgun 0 0
解题思路
\hspace{17pt} 这是一个纯模拟题,就是按照题目要求写代码即可。这个题的亮点是对输入数据的处理。我以前的处理方法是这样的:
while(cin>>names)
{
a[index].name=names;
for (int i=1; i<=n; i++)
{
string ss; cin>>ss;
if(ss=="0" || ss[0]=='-') continue;
int number=0;
a[index].acnum++;
for (int j=0; j<ss.size(); j++)
{
if(ss[j]>='0' && ss[j]<='9')
{
number*=10;
number+=ss[j]-'0';
}
else if(ss[j]=='(')
{
a[index].time+=number;
number=0;
}
else if(ss[j]==')')
{
a[index].time+=number*m;
number=0;
}
}
a[index].time+=number;
}
index++;
}
\hspace{17pt}
这种处理方法是一个字符一个字符处理,处理起来相当麻烦,而且码量很大,完全没有必要。
\hspace{17pt}
按照学长写的处理方法改进后是这样的:
while(scanf("%s",s)==1)
{
a[index].name=s;
for (int i=1; i<=n; i++)
{
scanf("%s",s);
if(sscanf(s,"%d(%d)",&x,&y)==1)
a[index].acnum+=(x>0),a[index].time += x > 0 ? x : 0;
else a[index].acnum++,a[index].time += x + y * m;
}
index++;
}
\hspace{17pt} 这种处理方法利用了sscanf函数,它的作用是从一个字符串中读进与指定格式相符的数据。函数原型为
int sscanf ( const char * s, const char * format, ...);
\hspace{17pt}
它从s中读取数据,格式是format,存储在后面的参数中,函数返回值是函数将返回成功赋值的字段个数。
\hspace{17pt}
与之对应的有一个函数fprintf,其函数原型为:
int fprintf ( FILE * stream, const char * format, ... );
\hspace{17pt}
它从后面的参数中按照格式format输出数据到流文件stream中(也可以输出到string中),函数返回值是输出到字符数。
\hspace{17pt}
这两种函数都可以极大地简化我们的代码,应该熟练掌握。
完整代码
//#pragma GCC optimize(2)//比赛禁止使用!
//#pragma G++ optimize(2)
//#include <bits/stdc++.h>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <climits>
#include <algorithm>
#include <queue>
#include <vector>
using namespace std;
const int maxn=100000+1;
struct Student
{
string name;
int acnum;
int time;
bool operator < (const Student& p ) const
{
if(acnum>p.acnum) return true;
else if(acnum==p.acnum){
if(time<p.time) return true;
else if(time==p.time){
return name<p.name;
}
}
return false;
}
};
Student a[maxn];
char s[15];
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
int n,m,x,y;
scanf("%d%d",&n,&m);
int index=0;
while(scanf("%s",s)==1)
{
a[index].name=s;
for (int i=1; i<=n; i++)
{
scanf("%s",s);
if(sscanf(s,"%d(%d)",&x,&y)==1)
a[index].acnum+=(x>0),a[index].time += x > 0 ? x : 0;
else a[index].acnum++,a[index].time += x + y * m;
}
index++;
}
sort(a,a+index);
for (int i=0; i<index; i++)
{
printf("%-10s %2d %4d\n",a[i].name.c_str(),a[i].acnum,a[i].time);
}
return 0;
}