Grading Students - HackerRank.

HackerLand University has the following grading policy:
  • Every student receives a  in the inclusive range from  to .
  • Any  less than  is a failing grade.
Sam is a professor at the university and likes to round each student's  according to these rules:
  • 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.
For example,  will be rounded to  but  will not be rounded because the
rounding would result in a number that is less than .
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. It should return an integer array consisting of rounded grades.
gradingStudents has the following parameter(s):
  • grades: an array of integers representing grades before rounding
Input Format
The first line contains a single integer, , the number of students.
Each line  of the  subsequent lines contains a single integer, , denoting student 's grade.
Constraints
Output Format
For each , 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
image
  1. Student  received a , and the next multiple of  from  is . Since , the student's grade is rounded to .
  2. Student  received a , and the next multiple of  from  is . Since , the grade will not be modified and the student's final grade is .
  3. Student  received a , and the next multiple of  from  is . Since , the student's grade will be rounded to .
  4. Student  received a grade below , so the grade will not be modified and the student's final grade is .





SOURCE CODE



import java.io.*;
import java.math.*;
import java.text.*;
import java.util.*;
import java.util.regex.*;

public class Solution {

/*
* Complete the gradingStudents function below.
*/
static int[] gradingStudents(int[] grades) {
/*
* Write your code here.
*/
int ar[]=new int[grades.length];
for(int i=0;i<grades.length;i++)
{
if(grades[i]<38)
ar[i]=grades[i];
else
{
int k=0;
for(int j=0;j<=4;j++)
{
if((grades[i]+j)%5==0)
{
k = j;
break;
}
}
if(k<3)
ar[i]=grades[i]+k;
else
ar[i]=grades[i];
}
}
return (ar);

}

private static final Scanner scan = new Scanner(System.in);

public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));

int n = Integer.parseInt(scan.nextLine().trim());

int[] grades = new int[n];

for (int gradesItr = 0; gradesItr < n; gradesItr++) {
int gradesItem = Integer.parseInt(scan.nextLine().trim());
grades[gradesItr] = gradesItem;
}

int[] result = gradingStudents(grades);

for (int resultItr = 0; resultItr < result.length; resultItr++) {
bw.write(String.valueOf(result[resultItr]));

if (resultItr != result.length - 1) {
bw.write("\n");
}
}

bw.newLine();

bw.close();
}
}





Click here to Download



OUTPUT








If you have any question then leave a comment below I will do my best to answer that question.

Comments

Popular posts from this blog

[Question 3] ISC 2017 Computer Practical Paper Solved – Caesar Cipher.

Pattern of your own name.

Designing Patterns - Print 'Z'.