본문 바로가기
Programming/>> Algorithm

[Lucky Algorithm] Grading Students

by 니키ᕕ( ᐛ )ᕗ 2017. 9. 27.

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. For each , round it according to the rules above and print the result on a new line.


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

public class Solution {

    static int[] solve(int[] grades){
        // Complete this function
        int[] result = new int[grades.length];
        
        for(int i=0; i<grades.length; i++){
            
            int nextMultipleNum = ((int) grades[i] / 5) + 1;
            int nextMultiple = nextMultipleNum * 5;
            
            if(grades[i] < 38) {
                result[i] = grades[i];
            } else {
                if(nextMultiple - grades[i] < 3) {
                    result[i] = nextMultiple; 
                } else {
                    result[i] = grades[i]; 
                }
            }
        }
        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("");
        

    }
}


댓글