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;
}

posted @ 2020-07-02 19:16  _Hsiung  阅读(46)  评论(0编辑  收藏  举报