The Electronic Computer Research Society is having its club exams! There are a total of 3 subjects: Information, Mathematics, and English. During exam season, the club is always filled with a competitive atmosphere. In this club, there's a special weighting system for grades.
Information scores must be multiplied by 5, Mathematics by 3, and English by 2. Finally, calculate the weighted average.
If there are tied scores, they will be ranked based on the order of Information, Mathematics, and English scores, respectively. If there is still a tie, the one with the lower student ID number will be ranked higher. Now, let me assist you in ranking the members of the Electronic Computing Society!
Sample Inputs/Outputs
Sample Input(s) | Sample Output(s) |
---|---|
Enter an integer N, representing the number of students participating in the exam, followed by N lines of input. Each line contains 4 integers A1, A2, A3, A4, representing the student's ID number, Information score, Mathematics score, and English score, respectively. | Output the student ID numbers and their averages in descending order of scores. Each student's information should be printed on a new line. |
7 1 60 80 50 2 80 40 90 3 50 70 80 4 60 80 70 5 100 20 20 6 60 40 100 7 60 80 50 | 2 70 4 68 1 64 7 64 6 62 3 62 5 60 |
Thought Process
Use integers to store the student ID and the three scores, then calculate the weighted average using the formula: [(資訊×5)+(數學×3)+(英文×2)] / (5+3+2). When calculating the average, ensure to treat the scores as floating-point numbers by converting them before performing the calculation.
Declare a Vector<Vector>, using a two-dimensional array for sorting, where each person is represented by a Vector. After receiving the data, sequentially push the following data into the Vector: weighted average (float), information score (integer), math score (integer), English score (integer), student ID * -1.
After sorting the two-dimensional array, you'll need to output it from right to left. However, if all scores are tied, the student with the higher student ID should be placed further to the right. By storing the student ID * -1 when saving the data, students with lower student IDs will be placed further to the right.
Next, use an if statement to check if the weighted average is divisible by 1. If it is, convert the previously calculated floating-point number to a string type. Then, using a for loop, add all the characters before the decimal point (excluding the decimal point itself) to a new string. Use a Map<Vector, string> where the key represents the current person's Vector, and the value represents the concatenated string obtained earlier for output purposes.
Otherwise, if the weighted average is not divisible by 1, perform the same steps as in the if statement, but this time, add the characters to the string after the decimal point, up to the first decimal place (without rounding).
When outputting, remember to multiply the student ID by -1 again. For the average value, simply use Map[stu[i]] to output the concatenated string that was created earlier.
Sample Code-ZeroJudge H075: Score
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
int main() {
cin.sync_with_stdio(0);
cin.tie(0);
int N;
cin >> N;
vector<vector<float>>stu;
map<vector<float>, string>MAP;
for (int i = 0; i<N; i++)
{
vector<float>v;
int id, com, math, eng;
cin >> id >> com >> math >> eng;
float tmp = (float(com*5) + float(math*3) + float(eng*2))/10.;
v.push_back(tmp);
v.push_back(com);
v.push_back(math);
v.push_back(eng);
v.push_back(id * -1);
if (((com*5) + (math*3) + (eng*2)) % 10 == 0)
{
string str = to_string(tmp), newstr = "";
for (int j = 0; j<str.length(); j++)
{
if (str[j] == '.') break;
newstr += str[j];
}
MAP[v] = newstr;
}
else
{
string str = to_string(tmp), newstr = "";
for (int j = 0; j<str.length(); j++)
{
newstr += str[j];
if (str[j] == '.')
{
newstr += str[j+1];
break;
}
}
MAP[v] = newstr;
}
stu.push_back(v);
}
sort(stu.begin(), stu.end());
for (int i = N-1; i>=0; i--)
{
cout << stu[i][4] * -1 << " " << MAP[stu[i]] << "\n";
}
}
//ZeroJudge H075
//Dr. SeanXD