10-排序5 PAT Judge (25 分)

  • Post author:
  • Post category:其他


#include <iostream>
#include <cstdio>
#include<vector>
#include <algorithm>
using namespace std;
const int maxn = 10001;
const int maxk = 6;
int gProblemTable[maxk];


struct userinfo {
	int ID;
	int totalscore;
	int allclearCount;
	int rank;
	int Problem[maxk];
	int evercompiled;//是否通过编译的标志,通过编译至少一道题才可以被打印
};
userinfo gUserInfo[maxn];

bool cmp(userinfo s1, userinfo s2) {
	//比较函数
	if (s1.totalscore != s2.totalscore)
	{
		return s1.totalscore > s2.totalscore;
	}
	else if (s1.allclearCount != s2.allclearCount)
	{
		return s1.allclearCount > s2.allclearCount;
	}
	else
	{
		return s1.ID < s2.ID;
	}
}

void InitSubmission(int N, int K) {//初始化
	for (int i = 1; i <= N; i++) {
		gUserInfo[i].allclearCount = 0;
		gUserInfo[i].evercompiled = 0;
		gUserInfo[i].rank = 0;
		gUserInfo[i].totalscore = 0;
		for (int j = 1; j <= K; j++)
		{
			gUserInfo[i].Problem[j] = -2;
		}
	}
}

void PrintInfo(userinfo u, int K) {
	//打印学生每道题目的分数
	for (int i = 1; i <= K; i++)
	{
		if (u.Problem[i] == -2) {
			printf(" -");
		}
		else {
			printf(" %d", u.Problem[i]);
		}
	}
	printf("\n");
}

int main() {
	int N, K, M;
	scanf("%d%d%d", &N, &K, &M);
	InitSubmission(N, K);
	for (int i = 1; i <= K; i++)
	{
		scanf("%d", &gProblemTable[i]);
	}
	for (int i = 0; i < M; i++)
	{	
		int ID, pId, score;
		scanf("%d%d%d", &ID, &pId, &score);
		gUserInfo[ID].ID = ID;
		if (score>gUserInfo[ID].Problem[pId])
		{	
			if (score==-1)//没有通过编译,该题得分为=0
			{
				gUserInfo[ID].Problem[pId] = 0;
			}
			else//编译通过
			{
				gUserInfo[ID].evercompiled = 1;
				gUserInfo[ID].Problem[pId] = score;	
			}
			if (score == gProblemTable[pId])
			{
				gUserInfo[ID].allclearCount++;
			}
		}
	}

	vector <userinfo> v;
	for (int i = 1; i <= N; i++)
	{
		for (int j = 1; j <= K; j++)
		{
			if (gUserInfo[i].Problem[j] == -2)
				continue;
			else {
				gUserInfo[i].totalscore += gUserInfo[i].Problem[j];
			}
		}
		
		if (gUserInfo[i].evercompiled==1)//编译通过的人可以排名
		{	
			v.push_back(gUserInfo[i]);
		}
	}
	sort(v.begin(), v.end(), cmp);
	v[0].rank = 1;
	printf("%d %05d %d", v[0].rank, v[0].ID, v[0].totalscore);
	PrintInfo(v[0], K);
	for (int i = 1; i < v.size(); i++)
	{
		if (v[i].totalscore == v[i - 1].totalscore) {
			v[i].rank = v[i - 1].rank;
		}
		else
		{
			v[i].rank = i + 1;
		}
		printf("%d %05d %d", v[i].rank, v[i].ID, v[i].totalscore);
		PrintInfo(v[i], K);
	}
	system("pause");
	return 0;
}




重点问题说明:

1.最开始初始化所有的题目分数为-2;从未提交过的题目最后输出“-”;提交但没有编译通过的输出0.

在这里插入图片描述

2.输入为-1表示未通过编译器,所输出的分数为0;

从来没有提交过和提交的题目都没有通过编译的人,不会列在排名上,因此列在排名上的人必须有一道通过编译,即使得分为0分。

3.上述代码中,设置vector来存储能够输出的人:只要编译通过过一道题目,就可以排名

4.只要提交过,便可计入总分,即使为0分。

5.主要使用了sort函数…技术含量比较低,也比较懒。

6.计算排名也是用了比较经典的方式,先定义第一名的排名再遍历。



版权声明:本文为weixin_40650422原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。