본문 바로가기

코딩 테스트

[Hacker Rank] (Easy) Grading Students

728x90

문제


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.

 

Examples

  •  round to  (85 - 84 is less than 3)
  •  do not round (result is less than 40)
  •  do not round (60 - 57 is 3 or higher)

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.

gradingStudents has the following parameter(s):

  • int grades[n]: the grades before rounding

 

Returns

  • int[n]: the grades after rounding as appropriate

 

Input Format

The first line contains a single integer, , the number of students.
Each line  of the  subsequent lines contains a single integer,

 

- 입력 예시 -

 

// (case 1)
4
73
67
38
33

 

 출력 예시  -

 

// (case 1)
75
67
40
33

정답 코드


<  내 정답 코드  >

 

class Result {
    private static final int LOWER_LIMIT = 38;

    public static List<Integer> gradingStudents(List<Integer> grades) {
        List<Integer> answer = new ArrayList<>();

        for (Integer grade : grades) {
            if (grade < LOWER_LIMIT) {
                answer.add(grade);
                continue;
            }
            if (grade % 5 == 0) {
                answer.add(grade);
                continue;
            }
            if (grade % 5 != 3 && grade % 5 != 4) {
                answer.add(grade);
                continue;
            }
            int extraPoint = grade % 5 == 3 ? 2 : 1;
            answer.add(grade + extraPoint);
        }

        return answer;
    }
}

 

<  타인 답변 코드  >

 

public class Problem2 {
    public static List<Integer> gradingStudents(List<Integer> grades) {
        List<Integer> answers = new ArrayList<>();
        for (int grade : grades) {
            if (grade < 38) {
            	answers.add(grade);
            } else if (grade % 5 > 2) {
            	answers.add(grade + (5 - grade % 5));
            } else {
            	answers.add(grade);
            }
        }
        
        return answers;
    }
}

이것을 주의하자!


-  논리는 어렵지 않았다. 하지만 구현하는데 하나씩 차근차근 구현했다.

 

-  타인의 코드를 보면 나와 비슷하지만, 나보다 훨씬 더 축약해서 작성했다. 저런 논리적 사고가 훈련이 필요할텐데 분발해야겠다.