Logu P1093 Scholarship

Posted by versatilewt on Thu, 30 Sep 2021 20:07:40 +0200

Catalog

Title Description

Input Format

Output Format

Analysis ideas:

Source code (passed):

 

Title Description

A primary school has recently been sponsored to award scholarships to the top five students with excellent academic performance. At the end of the term, each student has three courses: Chinese, Mathematics and English. First, the total score is sorted from high to low. If the total score of the two students is the same, then the total score is sorted from high to low. If the total score of the two students is the same, then the total score is sorted from high to low.What stipulates that students with small numbers are in the first place, so that the order of each student is uniquely determined.

Task: First calculate the total score based on the results of the three courses entered, then sort according to the above rules, and finally output the number and total score of the top five students in the ranking order. Note that among the top five students, each student has a different scholarship, so you must strictly sort according to the above rules. For example, in a correct answer, if the output data of the first two lines are(Output two numbers per line: number, total score) is:

77 279279
55 279279

The meaning of these two lines of data is: the two students with the highest total score are No. 77 and No. 55 respectively. The two students have a total score of 279279 (the total score equals the sum of the input Chinese, Mathematics and English scores), but the students with the school number 77 have higher Chinese scores. If your first two output data are:

55 279279
77 279279

Then it will be treated as output error and will not be scored.

Input Format

n+1 rows in total.

Behavior 11 A positive integer n (\le 300) n (< 300) indicates the number of students participating in the school's selection.

Lines 22 to n+1n+1 have 33 space-separated numbers on each line, each between 00 and 100100. The 33 numbers on line j j represent the student's performance in Chinese, math and English with the number j-1j_1 in turn. Each student's number is numbered from 1 to N1 n in the order in which it was entered (exactly the line number of the input data minus 11).

The data given is correct and does not need to be tested.

Output Format

There are five rows, each of which is a positive integer separated by a space representing the number and total score of the first 55 students.

Analysis ideas:

Heart ache, busy living overnight, anti-aircraft gun mosquitoes belong to.

First paste the source code I wrote myself:

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
//Encapsulate structure below
struct student
{
	int chinese = 0, math = 0, english = 0, no = 0,
		grade = 0, flag = 0;//flag to mark if the student has already output
};
void read(student &person)//Notice incoming references to really change person content
{
	scanf("%d %d %d", &person.chinese, &person.math, &person.english);
	person.grade = person.chinese + person.math + person.english;
}
void print(student p)
{
	printf("%d %d\n", p.no, p.grade);
}
bool cmp(int a,int b)
{
	return a > b;
}
student& vs(student &m1, student &m2)//Define the comparison function under the same score
{
	if (m1.chinese > m2.chinese)
		return m1;
	else if (m1.chinese < m2.chinese)
		return m2;
	else//If the score is equal
	{
		if (m1.no < m2.no)//Comparative School Number
			return m1;
		else
			return m2;
	}
}
bool check(student m,vector<int> p)//Check if the student has previously output (by number)
{
	for (auto a : p)
	{
		if (a == m.no)
			return false;
	}
	return true;
}
int a[301];
int main()
{
	int n;
	cin >> n;
	vector<student> all;
	vector<int> old;//Store student numbers that have been output
	student person;
	for (int i = 1; i <= n; ++i)
	{
		read(person);
		person.no = i;
		all.push_back(person);//Push student performance into container
		a[i] = person.grade;//Because the total score is sorted by the wait, the total score is placed in an array separately
	}
	sort(a + 1, a + n + 1,cmp);//Sort total score
	student nb[6];//Array holds the top five data
	int tag=0;//Used to mark whether there is an identical score
	for (int i = 1; i <= 5; ++i)//Find the top five
	{
		for (int j = 0; j < all.size(); ++j)
		{
			if (all[j].grade == a[i]&&check(all[j],old))//If someone with that score is found, and before
				//No output
			{
				++tag;
				if (tag == 1)//The information of the first person with that score can be put into the array first
					nb[i] = all[j];
				else//If you find someone with the same score as before
				{
					nb[i] = vs(nb[i], all[j]);//Use the rolling table to see who's ahead
					old.push_back(nb[i].no);
				}
			}
		}
		print(nb[i]);
	}
	return 0;
}

I used containers, customized a bunch of functions, and almost accessed them with a data structure pointer. The biggest problem with this is that I only know that sort functions can be row size, but I don't know that cmp can also be customized to define the sort order. It says that an array of structures can also be sorted in a customized way. Heartache, a busy evening, but I still review a little bit.Knowledge of functions and containers.

I can only write my own code to 70 points.

Here's the code that I modified when I knew sort could customize the sort:

Source code (passed):

#include<iostream>
#include<cstdio>
#include<vector>
#include<algorithm>
using namespace std;
//Encapsulate structure below
struct student
{
	int chinese = 0, math = 0, english = 0, no = 0,
		grade = 0;
};
void read(student &person)//Notice incoming references to really change person content
{
	scanf("%d %d %d", &person.chinese, &person.math, &person.english);
	person.grade = person.chinese + person.math + person.english;
}
void print(student p)
{
	printf("%d %d\n", p.no, p.grade);
}
bool cmp(student m1, student m2)
{
	if (m1.grade != m2.grade)
		return  m1.grade > m2.grade;
	if (m1.chinese != m2.chinese)
		return m1.chinese > m2.chinese;
	return m1.no < m2.no;
}
int main()
{
	int n;
	cin >> n;
	student p;
	vector<student> all;
	for (int i = 1; i <= n; ++i)
	{
		read(p);
		p.no = i;
		all.push_back(p);
	}
	sort(all.begin(), all.end(), cmp);
	for (int i = 0; i < 5; ++i)
	{
		print(all[i]);
	}
	return 0;
}

Almost double the amount of code required to complete the problem perfectly and easily by customizing the sorting by cmp.

A good idea, a good method, is really much more useful than simply meticulous and conscientious.

Topics: Algorithm