Grading students Hackerrank solution in cpp , c , python and java
Problem statement :
HackerLand University has the following grading policy .
- If the difference between the and the next multiple of is less than , round up to the next multiple of .
- If the value of is less than , no rounding occurs as the result will still be a failing grade.
Examples
- round to (85 - 84 is less than 3)
- do not round (result is less than 40)
- do not round (60 - 57 is 3 or higher)
Given the initial value of for each of Sam's students, write code to automate the rounding process.
Function Description
Complete the function gradingStudents in the editor below.
gradingStudents has the following parameter(s):
- int grades[n]: the grades before rounding
Returns
- int[n]: the grades after rounding as appropriate
Input Format
The first line contains a single integer, , the number of students.
Each line of the subsequent lines contains a single integer, .
Constraint
Sample Input 0
4
73
67
38
33
Sample Output 0
75
67
40
33
Explanation 0
- Student received a , and the next multiple of from is . Since , the student's grade is rounded to .
- Student received a , and the next multiple of from is . Since , the grade will not be modified and the student's final grade is .
- Student received a , and the next multiple of from is . Since , the student's grade will be rounded to .
- Student received a grade below , so the grade will not be modified and the student's final grade is .
Solution :
Code in CPP:
#include<iostream>
using namespace std ;
int main()
{
int n;
cin>>n;
int grade[n],i;
for ( i = 0; i < n; i++)
{
cin>>grade[i];
if ( grade[i]>=38)
{
int a=grade[i]/5;
if ( (a+1)*5-grade[i]<3)
{
grade[i]=(a+1)*5;
}
}
}
for ( i = 0; i < n; i++)
{
cout<<grade[i]<<endl;
}
}
Code in C :
#include<stdio.h>
int main()
{
int n;
scanf("%$d",&n);
int grade[n],i;
for ( i = 0; i < n; i++)
{
scanf("%d",&grade[i]);
if ( grade[i]>=38)
{
int a=grade[i]/5;
if ( (a+1)*5-grade[i]<3)
{
grade[i]=(a+1)*5;
}
}
}
for ( i = 0; i < n; i++)
{
printf("%d\n",grade[i]);
}
}
Code in Python :
import sys
n = int(input().strip())
for a0 in range(n):
grade = int(input().strip())
if grade >= 38 and grade % 5 > 2:
grade = -(-grade//5)*5
print(grade)
Code in Java :
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
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();
int newGrade = 0;
if(grade < 38)
System.out.println(grade);
else
{
int x = grade/5;
newGrade = (x+1)*5;
if(newGrade-grade < 3)
{
grade = newGrade;
}
System.out.println(grade);
}
}
}
}
Please comment if you have any doubt or for any other problem .
Thanks!!
0 Comments