PAT Basic Level 1080 MOOC final grade problem solving ideas and AC code v1.0

Keywords: data structure hash map

1. Topic description and online test location

1.1 the input is the user name and its score. Since the score includes three parts: online, midterm and end of the period, the input data of the same user name may contain multiple entries; The calculation rule of the final score is: on the premise that the online score is no less than 200, if the final score is greater than or equal to the mid-term score, the final score is the final score; If the interim score is greater than the final score, the final score is the interim score × 0.4 + final score × 0.6 and the decimal places are rounded
1.2 online test location: PAT 1080 MOOC final grade

2. Basic ideas

2.1 the user name is unique and the input data is out of order. It is necessary to establish the association between the user name and various grades. My idea is to establish a one-to-one correspondence between the user name and the array index through the container map, that is, to determine its corresponding unique array subscript through the user name, so as to cooperate with the structure array to complete the storage of input data

struct Student
{
	string Name;
	int OnlineScore;
	int MidScore;
	int EndScore;
	int Sum;
};
struct Student Stu[MAX];

map<string, int> Relation;
int Index, Count = 1;

Relation[Name] = Count++;
Index = Relation[Name];

2.2 after the input data is stored, it is easier to process. The final score of each person is calculated by coding according to the scoring rules, and then sorted through the sort() function (the first priority is the final score, and the second priority is the dictionary order of the user name). Finally, the student information that meets the qualified conditions can be printed

bool Compare(struct Student S1, struct Student S2)
{
	if (S1.Sum != S2.Sum)
		return S1.Sum > S2.Sum;
	else
		return S1.Name < S2.Name;
}

2.3 there are two points to note: the mid-term score of the current period is greater than the final score, and the final score is the mid-term score × 0.4 + final score × 0.6, the result needs to be rounded

Stu[i].Sum = Stu[i].MidScore * 0.4 + Stu[i].EndScore * 0.6 +0.5 ; 

The number of onlinestu, midstu and endstu does not exceed 10000, so the maximum is 30000. Therefore, the size of the structure array needs to be set larger

#define MAX 20000 

3. Complete AC code

#include <algorithm>
#include <map>
#include <iostream>
using namespace std;

#define MAX 20000 / / test points: onlinestu, midstu and endstu. The maximum number of each point is 30000

struct Student
{
	string Name;
	int OnlineScore;
	int MidScore;
	int EndScore;
	int Sum;
};

bool Compare(struct Student S1, struct Student S2)
{
	if (S1.Sum != S2.Sum)
		return S1.Sum > S2.Sum;
	else
		return S1.Name < S2.Name;
}


int main()
{
	map<string, int> Relation; int Index, Count = 1; //Determine the corresponding relationship between Name and Array Index through map

	int OnlineStu, MidStu, EndStu;
	cin >> OnlineStu >> MidStu >> EndStu;
	string Name; int Score;

	//struct Student* Stu = (struct Student*)malloc(sizeof(struct Student) * Max+1);
	//It's strange that there are many inexplicable errors when using malloc. You need to switch malloc to new

	struct Student Stu[MAX];
	for (int i = 0; i < MAX; i++) //initialization
	{
		Stu[i].OnlineScore = -1;
		Stu[i].MidScore = -1;
		Stu[i].EndScore = -1;
		Stu[i].Sum = -1;
	}

	while (OnlineStu--)
	{
		cin >> Name >> Score;

		Relation[Name] = Count++;
		Index = Relation[Name];

		Stu[Index].Name = Name;

		if (Score <= 900 && Score>=0)
			Stu[Index].OnlineScore = Score;
	}

	while (MidStu--)
	{
		cin >> Name >> Score;

		if(!Relation[Name]) //First appearance of user name
			Relation[Name] = Count++;
		Index= Relation[Name];

		Stu[Index].Name = Name;

		if (Score <= 100 && Score>=0)
			Stu[Index].MidScore = Score;
		
	}

	while (EndStu--)
	{
		cin >> Name >> Score;

		if (!Relation[Name]) //First appearance of user name
			Relation[Name] = Count++;
		Index = Relation[Name];

		Stu[Index].Name = Name;

		if (Score <= 100 && Score>=0)
			Stu[Index].EndScore = Score;
	}

	//Data processing
	for (int i = 1; i < Count; i++)
	{
		if (Stu[i].OnlineScore >= 200)
		{
			if (Stu[i].EndScore >= Stu[i].MidScore ) //
				Stu[i].Sum = Stu[i].EndScore;
			else //Test point: pay attention to rounding
				Stu[i].Sum = Stu[i].MidScore * 0.4 + Stu[i].EndScore * 0.6 +0.5 ;
		}
	}

	sort(Stu+1,Stu+Count,Compare); //Index start from 1!

	for (int i = 1; i < Count; i++)
	{
		if (Stu[i].Sum >= 60)
		{
			cout << Stu[i].Name << " " << Stu[i].OnlineScore << " "
				<< Stu[i].MidScore << " " << Stu[i].EndScore << " "
				<< Stu[i].Sum;
			cout << endl;
		}
		else
			break;
	}

	return 0;
}

Posted by mrphobos on Fri, 19 Nov 2021 08:37:49 -0800