A1075 PAT Judge [排序]

在这里插入图片描述

思路:

  1. 这题意思是连续的学号,我一开始以为是分开的学号,然后搞得数组很大,超时了一个测试点,后面都改了一遍。还是挺有难度的。
  2. 逻辑要清晰点,条件要写齐。比如这两个条件我就有遗忘,理所当然了。先拿输入的成绩比较,最后再换这样分开好一些。蓝瘦香菇鸭~

在这里插入图片描述

——————————————————————————————————————————————————

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
struct student
{
	int id;
	int score[6];
	int fullcount = 0;
	bool flag = false;
	int allscore = 0;
	int rank;
}stu[100001];
bool cmp(student a, student b)
{
	if (a.allscore != b.allscore)
		return a.allscore > b.allscore;
	else if (a.fullcount != b.fullcount)
		return a.fullcount > b.fullcount;
	else return a.id < b.id;
}
int full[5];
int main()
{
	int n, k, m;
	cin >> n >> k >> m;
	for (int i = 1; i <= n; i++)
	{
		stu[i].id = i;
		memset(stu[i].score, -1, sizeof(stu[i].score));
	}
	for (int i = 1; i <= k; i++)
	{
		cin >> full[i];
	}
	int id = 0, testid = 0, score = -1;
	for (int i = 0; i < m; i++)
	{
		cin >> id >> testid >> score;
		if (score >= 0)
		{
			stu[id].flag = 1;
		}
		if (score == -1&&stu[id].score[testid]==-1)           
		{
			stu[id].score[testid] = 0;
		}
		if (score == full[testid]&&stu[id].score[testid]<full[testid])
		{
			stu[id].fullcount++;
		}
		if (score > stu[id].score[testid])
		{
			stu[id].score[testid] = score;
		}
	}
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j <= k; j++)
		{
			if (stu[i].score[j] != -1) {
				stu[i].allscore += stu[i].score[j];
			}
		}
	}
	sort(stu+1, stu + n+1, cmp);
	stu[1].rank = 1;
	for (int i = 2; i <= n&&stu[i].flag; i++)
	{
		if (stu[i].allscore == stu[i - 1].allscore)
		{
			stu[i].rank = stu[i - 1].rank;
		}
		else
		{
			stu[i].rank = i;
		}
	}
	for(int i = 1; i <=n; i++)
	{
		if (stu[i].flag)
		{
			cout << stu[i].rank;
			printf(" %05d %d",stu[i].id,stu[i].allscore);
			for (int j = 1; j <= k; j++)
			{
				if (stu[i].score[j] >= 0)
					cout << " " << stu[i].score[j];
				else
					cout << " " << "-";
			}
			cout << endl;
		}
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值