Problem
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
- 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 .
How I solved the problem
# 조건을 잘 파악하는게 중요!
# 현재 성적 바로 다음의 5의 배수는 -> (현재성적 / 5 ) + 1을 해주고 이 값에 5를 곱해주면 됨!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | 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 gradeNum = grades.length; int[] result = new int[gradeNum]; for (int i = 0; i < gradeNum; i++) { int multipleNum = (grades[i] / 5) + 1; int compNum = multipleNum * 5; //grade should be more than 38 and the gap between multiple of 5 and grades //should be less than 3 if (grades[i] >= 38 && compNum - grades[i] < 3) { result[i] = compNum; } else { result[i] = grades[i]; } } return result; } 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(); } } | cs |
[출처 : https://www.hackerrank.com ]
'1 Day 1 Algorithms' 카테고리의 다른 글
[2019.01.30] Kangaroo (0) | 2019.01.30 |
---|---|
[2019.01.29] Apple and Orange (0) | 2019.01.29 |
[2019.01.25] Time Conversion (0) | 2019.01.25 |
[2019.01.24] Birthday Cake Candles (0) | 2019.01.24 |
[2019.01.23] Bitwise AND (1) | 2019.01.23 |
댓글