题意:
程序设计思维作业和实验使用的实时评测系统,具有及时获得成绩排名的特点,那它的功能是怎么实现的呢?
我们编写完程序并提交以后,评测系统要么返回AC,要么是返回各种其他的错误,不论是怎样的错法,它都会记录你的一次你的错误提交,而当你历经千辛终将它AC之后,它便会显示这题共错误提交了几次。
在岁月的长河中,你通过的题数虽然越来越多,但通过每题时你所共花去的时间(从最开始算起,直至通过题目时的这段时间)都会被记录下来,作为你曾经奋斗的痕迹。特别的,对于你通过的题目,你曾经的关于这题的每次错误提交都会被算上一定的单位时间罚时,这样一来,你在做出的题数上,可能领先别人很多,但是在做出同样题数的人中,你可能会因为罚时过高而处于排名上的劣势。
例如某次考试一共八道题(A,B,C,D,E,F,G,H),每个人做的题都在对应的题号下有个数量标记,负数表示该学生在该题上有过的错误提交次数但到现在还没有AC,正数表示AC所耗的时间,如果正数a跟上了一对括号,里面有个正数b,则表示该学生AC了这道题,耗去了时间a,同时曾经错误提交了b次。例子可见下方的样例输入与输出部分。
输入:
输入数据包含多行,第一行是共有的题数n(1≤n≤12)以及单位罚时m(10≤m≤20),之后的每行数据描述一个学生的信息,首先是学生的用户名(不多于10个字符的字串)其次是所有n道题的得分现状,其描述采用问题描述中的数量标记的格式,见上面的描述。
Simple 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
输出:
根据这些学生的得分现状,输出一个实时排名。实时排名显然先按AC题数的多少排,多的在前,再按时间分的多少排,少的在前,如果凑巧前两者都相等,则按名字的字典序排,小的在前。每个学生占一行,输出名字(10个字符宽),做出的题数(2个字符宽,右对齐)和时间分(4个字符宽,右对齐)。名字、题数和时间分相互之间有一个空格。数据保证可按要求的输出格式进行输出。
Sample Output:
TT 5 348
yjq 4 342
GuGuDong 3 197
hrz 3 256
Zjm 2 316
OMRailgun 0 0
思路:
建立一个选手类型的结构体,其中用一个char数组保存名字,两个整型分别保存ac的题数和当前ac题目的总耗时,一个long long型二维数组保存ac使用时间与错误提交次数。编写cmp函数,用于比较不同选手,以得出最终排名。按格式输入后,使用sort()函数进行排名,在按格式输出。
代码:
#include <iostream>
#include <stdio.h>
#include <string>
#include<string.h>
#include <vector>
#include<algorithm>
using namespace std;
struct peo{
char name[15];
int numofac,time;
long long **a;
int numofques;
peo(char*thename,int n)
{
numofques=n;
numofac=0;
time=0;
strcpy(name,thename);
a=new long long*[n];
for(int i=0;i<n;i++)
{
a[i]=new long long[2];
}
}
peo(const peo&b)
{
strcpy(name,b.name);
this->numofac=b.numofac;
this->time=b.time;
this->numofques=b.numofques;
a=new long long*[numofques];
for(int i=0;i<numofques;i++)
{
a[i]=new long long[2];
}
for(int i=0;i<numofques;i++)
{
for(int j=0;j<2;j++)
a[i][j]=b.a[i][j];
}
}
};
bool cmp(peo&a,peo&b)
{
if(a.numofac!=b.numofac)
{
return a.numofac>b.numofac;
}
else if(a.time!=b.time)
{
return a.time<b.time;
}
else
{
return strcmp(a.name,b.name)<0;
}
}
int main(int argc, char** argv) {
int n,m;
scanf("%d %d",&n,&m);
//getchar();
vector<peo> a;
char name[10];
while(scanf("%s",name)!=EOF)
{
peo temp(name,n);
string str;
for(int i=0;i<n;i++)
{
cin>>str;
if(str[str.size()-1]==')')
{
sscanf(str.c_str(),"%lld(%lld)",&temp.a[i][0],&temp.a[i][1]);
temp.numofac++;
temp.time+=(temp.a[i][0]+temp.a[i][1]*m);
}
else{
sscanf(str.c_str(),"%lld",&temp.a[i][0]);
if(temp.a[i][0]>0)
{
temp.numofac++;
temp.time+=temp.a[i][0];
}
}
}
a.push_back(temp);
//getchar();
}
sort(a.begin(),a.end(),cmp);
//vector<peo>::iterator it=a.begin();
//printf("%-10s %2lld %4lld",(*it).name,(*it).numofac,(*it).time);
for(vector<peo>::iterator it=a.begin();it!=a.end();it++)
{
printf("%-10s %2lld %4lld\n",(*it).name,(*it).numofac,(*it).time);
}
return 0;
}