[HackerRank] Grading Students c++
- Problem
HackerLand University has the following grading policy:
- Every student receives a grade in the inclusive range from 0 to 100 .
- Any grade less than 40 is a failing grade.
Sam is a professor at the university and likes to round each student's grade according to these rules:
- If the difference between the and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
- If the value of grade is less than 38 grade, no rounding occurs as the result will still be a failing grade.
- Example
grade = 84 round to 85 (85 - 84 is less than 3)
grade = 29 do not round (result is less than 40)
grade = 57 do not round (60 - 57 is 3 or higher)
Given the initial value of grade for each of Sam's n students, write code to automate the rounding process.
- Constraints
1<= n <= 60
0<= grades[i] <= 100
- Sample Input
4
73
67
38
33
- Sample Output
75
67
40
33
- Explanation
ID | Original Grade | FInal Grade |
1 | 73 | 75 |
2 | 67 | 67 |
3 | 38 | 40 |
4 | 33 | 33 |
- Student 1 received a 73, and the next multiple of 5 from 73 is 75. Since 75-73<3, the student's grade is rounded to .
- Student 2 received a 67, and the next multiple of 5 from 67 is 70. Since 70-67 = 3, the grade will not be modified and the student's final grade is 67.
- Student 3 received a 38, and the next multiple of 5 from 38 is 40. Since 40-38<3, the student's grade will be rounded to 40.
- Student 4 received a grade below 33, so the grade will not be modified and the student's final grade is 33.
- Solutions
vector<int> gradingStudents(vector<int> grades) {
vector<int> res;
for(auto &it : grades){
int n = 0, tmp = 0;
n = it % 5;
tmp = 5 - n;
if(tmp >= 3) res.push_back(it);
else if(it+tmp >= 40){
res.push_back(it+tmp);
}
else{
res.push_back(it);
}
}
return res;
}