At HackerLand University, a passing grade is any grade 40 points or higher on a 100 point scale. Sam is a professor at the university and likes to round each student’s grade according to the following rules:
- If the difference between the grade and the next higher multiple of 5 is less than 3, round to the next higher multiple of 5
- If the grade is less than 38, don’t bother as it’s still a failing grade
Automate the rounding process then round a list of grades and print the results.
Input Format
The first line contains a single integer denoting (the number of students).
Each line of the subsequent lines contains a single integer, , denoting student 's grade.
Each line of the subsequent lines contains a single integer, , denoting student 's grade.
Constraints
Output Format
For each of the grades, print the rounded grade on a new line.
Sample Input 0
4
73
67
38
33
Sample Output 0
75
67
40
33
Explanation 0
The first grade, is two below the next higher multiple of , so it rounds to .
is points less than the next higher multiple of so it doesn’t round.
, like , rounds up to next higher multiple of , or in this case.
is less than , so it does not round.
is points less than the next higher multiple of so it doesn’t round.
, like , rounds up to next higher multiple of , or in this case.
is less than , so it does not round.
----------------------------------------------------------------------------------------
Grading Students - Hacker Rank Solution
------------------------------------------------------------------------------------------
Solution
As long as , using allows us to determine when we need to round a grade. We can then either:
1. Use a while loop to increment to the next multiple of .
Python 2
n = int(raw_input().strip())
for a0 in xrange(n):
grade = int(raw_input().strip())
if grade>=38 and grade%5>=3:
while grade%5!=0:
grade = grade + 1
print grade
C++
#include <bits/stdc++.h>
using namespace std;
void solution() {
int n, x;
cin>>n;
for(int i=0; i<n; i++){
cin>>x;
if(x>=38 and x%5>=3){
while(x%5!=0){
x++;
}
}
cout<<x<<endl;
}
}
int main () {
solution();
return 0;
}
2. Add the result of to .
Python 2
n = int(raw_input().strip())
for a0 in xrange(n):
grade = int(raw_input().strip())
if grade >= 38 and grade % 5 >= 3:
grade = grade + 5 - (grade % 5)
print grade
Or, even more efficiently:
Python 3
n = int(input().strip())
for a0 in range(n):
grade = int(input().strip())
if grade >= 38:
# Here, we are only ever calculating 'grade mod 5' once:
mod5 = grade % 5
if mod5 >= 3:
grade = grade + (5 - mod5)
print(grade)
Problem Setter's code:
C++
#include <bits/stdc++.h>
#include<assert.h>
using namespace std;
void solution() {
int n, x;
cin >> n;
assert(n > 0 && n <= 60);
for(int i = 0; i < n; i++)
{
cin >> x;
assert(x >= 0 && x <= 100);
if(x >= 38)
{
int y = x;
while(1)
{
if(y % 5 == 0)
break;
y++;
}
if(y - x <= 2)
x = y;
}
cout << x << endl;
}
}
int main () {
solution();
return 0;
}
Problem Tester's code:
import java.util.*;
public class Solution {
public static int getRoundedGrade(int grade) {
if (grade >= 38) {
int mod5 = grade % 5;
if (mod5 > 2) {
grade += 5 - mod5;
}
}
return grade;
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int n = in.nextInt();
for(int a0 = 0; a0 < n; a0++){
int grade = in.nextInt();
System.out.println(getRoundedGrade(grade));
}
in.close();
}
}
----------------------------------------------------------------------------------------
Grading Students - Hacker Rank Solution
-----------------------------------------------------------------------------------------
Good article
ReplyDeleteyour code for c++ is wrong.
ReplyDelete