# [C + +] [problem solution] performance ranking

Posted by phpnewbiy on Sat, 22 Jan 2022 07:50:35 +0100

# subject

### Title Description

The new year is coming!

As a CEO with n employees, it's time for you to rank the employees' performance annually.

Through the indicators of n employees this year (skill p, communication r, cooperation s, project t),
Calculate the annual performance of each employee, and output everyone's name from high to low.

Performance is calculated as follows:
trade performance = 40 % p + 30 % r + 20 % s + 10 % t Performance = 40\%p + 30\%r + 20\%s + 10\%t Performance = 40%p+30%r+20%s+10%t

### Input format

The first line is a positive integer n ( n ≤ 10000 ) n(n \leq 10000) n(n≤10000).

Next, there are n rows. Each row corresponds to the information of an employee. Format is

name p r s t


Where name is an English name with no more than 20 spaces. p. r, s and t are positive integers not greater than 1000 respectively.

### Output format

Output n lines, each line a person's name.

There is no space at the end of the line.

# Problem solution

#include<iostream>
#include<string>
#include<algorithm>
#include<functional>
using namespace std;
const int N = 10010;
class Employee{
private:
int q,r,s,t;
public:
string name;
bool operator>(const Employee &a) const;
friend istream& operator>>(istream& in,Employee& a);
};

Employee E[N];

bool Employee::operator>(const Employee &a) const{
int r1 = this->q * 4 + this->r * 3 + this->s * 2 + this->t;
int r2 = a.q * 4 + a.r * 3 + a.s * 2 + a.t;
if(r1 == r2)
return this->name < a.name;
return r1 > r2;
}

istream& operator>>(istream& in,Employee& a){
in >> a.name >> a.q >> a.r >> a.s >> a.t;
return in;
}

int main(){
cin.tie(0);
cout.tie(0);
ios::sync_with_stdio(false);
int n;
cin >> n;
for(int i = 0;i < n;i++)
cin >> E[i];
sort(E,E + n,greater<Employee>());
for(int i = 0;i < n;i++)
cout << E[i].name << endl;
return 0;
}


#### Lines 7 to 16

class Employee{
private:
int q,r,s,t;
public:
string name;
bool operator>(const Employee &a) const;
friend istream& operator>>(istream& in,Employee& a);
};

Employee E[N];


Because each employee has more information, it is better to use structure or class as storage structure.

As shown in the figure, we can define an array of employee classes, and the employee information is stored in the corresponding position of each element.

It should also be mentioned here that compared with the structure of C language, C + + classes can not only build a "composite" structure, but also define "exclusive functions" - member functions for this structure.

#### Lines 12, 13 and 18 to 29

bool operator>(const Employee &a) const;
friend istream& operator>>(istream& in,Employee& a);

bool Employee::operator>(const Employee &a) const{
int r1 = this->q * 4 + this->r * 3 + this->s * 2 + this->t;
int r2 = a.q * 4 + a.r * 3 + a.s * 2 + a.t;
if(r1 == r2)
return this->name < a.name;
return r1 > r2;
}

istream& operator>>(istream& in,Employee& a){
in >> a.name >> a.q >> a.r >> a.s >> a.t;
return in;
}


This is an overload of the operator greater than sign >. And overloading the shift right operator > >.

The overload greater than operator specifies how to perform greater than operation on two Employee objects, so that the greater than operator can be directly used for comparison between two Employee objects.

The shift right operator is overloaded to facilitate input, so that you can directly use CIN > > e [i]; The implementation sends data directly from the input stream to the Employee object.

#### Line 39

sort(E,E + n,greater<Employee>());


Library functions are used here for comparison.
The sort function is defined in the algorithm header file. Remember to include it before use.

The sort function has three functions,

The first parameter corresponds to the position of the first address of the array, the second parameter corresponds to the next position of the address of the last element of the array, and the third parameter can be omitted. By defau lt, the less than operator < is used to compare elements and sort them in ascending order.
If you want to sort in descending order, you need to specify the third function as greater < >. The greater than sign operator > between elements is used for comparison.
Reference materials: How to use SORT function in C + +

Similar to this problem, when the information is huge and the processing is complex, we should have the thinking of using object-oriented design method to reduce the difficulty of programming.

Original is not easy, thank you for your support.

Topics: C++ Back-end Class OOP