C++ Find classmates in grade list

【Problem Description】

Write a program to read the names of N students and their individual subject scores in three subjects except language and mathematics, and sort them according to certain sorting rules to form a score table (first sort by total score from high to low, the total score is the same Then sort by the Chinese scores from high to low, if they are still the same, sort by the math scores from high to low, if the total score and single subject score are the same, then sort by the dictionary order of the name). Finally, given a number K (K<=N), output the name and total score of the student at position K in this sorted score table.

Examples of lexicographic order:

abc > abd
a > aa

【Input form】

In the first line, enter a positive integer N (1<=N<=1024), which means that the scores of N students will be entered next.

The next N lines enter N student performance records. Each record is the name, Chinese score, math score, and English score in order. These four fields are separated by spaces. The name is a string (containing only lowercase letters, no spaces, length <=19), and there will be no duplicate names among students. The scores of the three subjects are all integers (0~100).

Finally, enter a positive integer K (1<=K<=N), which represents the position to be found from front to back in the grade list.

[Output form]

The names and total scores of the students in position K from front to back in the grade list, separated by spaces.

[Sample input 1]

2
tom 90 91 92
lucy 91 90 92
1

[Sample output 1]

lucy 273

[Sample input 2]

4
lily 90 100 88
jack 87 79 95
hanz 90 89 71
david 90 89 71
4

[Sample output 2]

hanz 250

[Detailed code implementation]

#include<iostream>
#include<string> 
#include<algorithm> //可以直接利用STL中的sort() 是结合了快速排序与插入排序的一种排序
using namespace std;
class Students
{
public:
	string  student_name;
	float chinese;
	float math;
	float english;
    float sum;
};

bool cmp(const Students& a,const Students& b)
{
    if (a.sum>b.sum)
        return true;
    else if (a.chinese>b.chinese && a.sum==b.sum)
        return true;
    else if (a.math>b.math && a.chinese==b.chinese && a.sum==b.sum)
        return true;
    else if (a.english>b.english && a.math==b.math && a.chinese==b.chinese && a.sum==b.sum)
        return true;
    else if ( a.student_name <b.student_name && a.english==b.english && a.math==b.math && a.chinese==b.chinese && a.sum==b.sum)
        return true;
    else
        return false;
}

int main()
{
	int N=1025;
	
	Students students[N];
	
	int K;
	int n;
	cin>>n;
    for (int i=0; i<n; i++)
    {
        cin>>students[i].student_name;
        cin>>students[i].chinese;
		cin>>students[i].math;
		cin>>students[i].english;
        students[i].sum=students[i].math+students[i].english+students[i].chinese;
    }
    sort(students,students+n,cmp);
	cin>>K;
    cout<<students[K-1].student_name<<" "<<students[K-1].sum;
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_74287172/article/details/134231976