Tuesday, February 27, 2018

HackerRank - Grading Students Problem

This is a HackerRank Problem. You can find it HERE

Question

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. ... Get details HERE

Solution


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

public class Solution {

    static int[] solve(int[] grades){
        int[] result = new int[grades.length];
        for (int i = 0; i < grades.length; i++) {
            if (grades[i] < 38) {
                result[i] = grades[i];
            } else {
                int rem = grades[i] % 5;
                if (rem < 3) {
                    result[i] = grades[i];
                } else {
                    result[i] = grades[i] + 5 - rem;
                }
            }
        }
        return result;
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[] grades = new int[n];
        for(int grades_i=0; grades_i < n; grades_i++){
            grades[grades_i] = in.nextInt();
        }
        int[] result = solve(grades);
        for (int i = 0; i < result.length; i++) {
            System.out.print(result[i] + (i != result.length - 1 ? "\n" : ""));
        }
        System.out.println("");
    }
}








No comments:

Post a Comment