Week2:OJ分数排名系统模拟

B-实时评测系统

题目内容:
程序设计思维作业和实验使用的实时评测系统,具有及时获得成绩排名的特点,那它的功能是怎么实现的呢?
我们千辛万苦怼完了不忍直视的程序并提交以后,评测系统要么返回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道题的得分现状,其描述采用问题描述中的数量标记的格式,见上面的表格。
输出格式
根据这些学生的得分现状,输出一个实时排名。实时排名显然先按AC题数的多少排,多的在前,再按时间分的多少排,少的在前,如果凑巧前两者都相等,则按名字的字典序排,小的在前。每个学生占一行,输出名字(10个字符宽),做出的题数(2个字符宽,右对齐)和时间分(4个字符宽,右对齐)。名字、题数和时间分相互之间有一个空格。数据保证可按要求的输出格式进行输出。
样例

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

TT          5  348
yjq         4  342
GuGuDong   3  197
hrz         3  256
Zjm         2  316
OMRailgun   0    0

(备注:不会在CSDN里打空格…控制台的输出是长得很标致的)

解题思路
模拟题。首先输入题目成绩的时候需要判断当前输入的数:

  • 一个正整数
  • 一个负整数
  • 一个正整数后面带了个括号,括号中间有个正整数

三种不同的情况需要在输入的时候进行判断。
其次是题目中说到的时间分。时间分的计算方式为:

时间分=所有AC消耗的时间+m*所有括号里的数字

至于输出,可以采取printf进行诸如%10d,%4d这样的输出
这里采取判断具体大小加空格的方式cout (水平太蔡)
以下是AC代码:

#include<iostream>
#include<string>
#include<cstring>
#include<vector>
#include<algorithm>

using namespace std;

int n, m;

struct Student
{
	string name;
	int ACnum;
	int timeGrades;
	
};

vector<Student>student;

bool cmp(const Student& a, const Student& b)
{
	if (a.ACnum != b.ACnum) return a.ACnum > b.ACnum;
	else if (a.timeGrades != b.timeGrades) return a.timeGrades < b.timeGrades;
	else return a.name < b.name;
}

void Input(string &tem)
{
	
	int l = tem.length();

	Student one;
	one.name = tem;
	one.ACnum = 0;
	one.timeGrades = 0;
	for (int i = 10; i > l; i--) one.name += ' ';

	bool isCrime;
	for (int i = 1; i <= n; i++)
	{
		cin >> tem;
		if (tem[0] != '-' && tem[0] != '0') one.ACnum++;
		else continue;

		int crimeCount = 0;
		int crimelength = 0;
		isCrime = false;
		int x;
		for (int j = 0; j < tem.length(); j++)
		{
			if (tem[j] == '(')
			{
				isCrime = true;
				x = j;
				continue;
			}

			if (isCrime&&tem[j]!=')')
			{
				crimelength++;
			}
		}

		if (!isCrime) one.timeGrades += atoi(tem.c_str());
		else
		{
			one.timeGrades += atoi((tem.substr(0, x)).c_str());
		}

		if (isCrime)
		{
			string tem2 = tem.substr(tem.find('(')+1, crimelength);
			crimeCount = atoi(tem2.c_str());
			one.timeGrades += crimeCount * m;
		}
	}

	student.push_back(one);

}

int main()
{
	cin >> n >> m;
	string tem;
	while (cin >> tem)
	{
		Input(tem);
	}

	sort(student.begin(), student.end(), cmp);

	for (int i = 0; i < student.size(); i++)
	{
		cout << student[i].name<<' ';
		if (student[i].ACnum >= 0 && student[i].ACnum <= 9)
		{
			cout << ' ' << student[i].ACnum << ' ';
		}
		else
		{
			cout << student[i].ACnum << ' ';
		}

		if (student[i].timeGrades >= 0 && student[i].timeGrades <= 9)
			cout << "   " << student[i].timeGrades << endl;
		else if (student[i].timeGrades >= 10 && student[i].timeGrades <= 99)
			cout << "  " << student[i].timeGrades << endl;
		else if (student[i].timeGrades >= 100 && student[i].timeGrades <= 999)
			cout << ' ' << student[i].timeGrades << endl;
		else cout << student[i].timeGrades << endl;
	}
}
发布了6 篇原创文章 · 获赞 2 · 访问量 58

猜你喜欢

转载自blog.csdn.net/qq_44506233/article/details/104692036
今日推荐