HR - Graduating Students

Goody·2021년 2월 2일
0

알고리즘

목록 보기
23/122

문제

HackerLand University has the following grading policy:

Every student receives agrade in the inclusive range from 0 to 100.
Any grade less than 40 is a failing grade.

Sam is a professor at the university and likes to round each student's grade according to these rules:

  • If the difference between the grade and the next multiple of 5 is less than 3, round grade up to the next multiple of 5.
  • If the value of grade is less than 38, no rounding occurs as the result will still be a failing grade.

Examples

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

예시

//Input
4
73
67
38
33
//Output
75
67
40
33

풀이

  • 학생의 학점과 가장 (큰쪽으로) 가까운 5의 배수와의 차가 3 미만이면 올림을 하는 문제이다.
  • 학점 배열과 5의 배수 배열을 이중 루프로 돌면서 비교하면 될 것 같다.

코드

function gradingStudents(grades) {
    const multipleOf5 = [40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100];
   
    for(let i = 0; i < grades.length; i++) {
        for(let j = 0; j < multipleOf5.length; j++) {
            if(multipleOf5[j] - grades[i] < 3 && multipleOf5[j] - grades[i] > 0) {
                grades[i] = multipleOf5[j];
            }
        } 
    }

    return grades;
}

0개의 댓글