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("");
}
}'Programming > >> Algorithm' 카테고리의 다른 글
| [Lucky Algorithm] Compare the Triplets (0) | 2017.10.03 |
|---|---|
| [Lucky Algorithm] Simple Array Sum (0) | 2017.10.03 |
| [Lucky Algorithm] Mini-Max Sum (0) | 2017.10.03 |
| [Lucky Algorithm] 시작 (0) | 2017.09.27 |
| [Algorithm] 혼자 결론 내린 정렬 알고리즘 (0) | 2017.08.30 |
댓글