본문 바로가기

문제풀이/C 문제풀이

[HackerRank]Grading Students

문제

HackerLand University has the following grading policy:
Every student receives a grade 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.

Function Description
Complete the function gradingStudents in the editor below. It should return an integer array consisting of rounded grades.
Input Format
The first line contains a single integer, n, the number of students.
Each line i of the n subsequent lines contains a single integer, grades[i], denoting student i's grade.
Output Format
For each grades[i], print the rounded grade on a new line.

CODE

int* gradingStudents(int grades_count, int* grades, int* result_count) {
    int *a = malloc(grades_count * sizeof(int)); // 동적 할당
    *result_count = grades_count; // 여기서는 쓰이지 않지만 밑의 코드에 필요하다

    for(int i = 0; i < grades_count; i++){
        if ((*(grades+i)>=38) && ((*(grades+i)%5)>2)) *(a+i)=(*(grades+i)/5+1)*5; // 반올림 조건을 만족하면 반올림 한다
        else *(a+i) = *(grades+i); // 그 외에는 점수 그대로 저장한다
    }
    return a;
}

감상

문제 해석이 어려웠다. 띄엄띄엄 예제를 보면서 문제를 이해했더니 작성해야할 함수 매개변수의 쓰임이 뭔지 헤맸다. 전체 함수를 대충 이해하고 나서 코드를 작성했다. 코드 자체의 논리는 별로 어렵지 않았지만 내 독해 능력이 부족했다. 그리고 /랑 %랑 헷갈려서 잠시 헤맸다.

'문제풀이 > C 문제풀이' 카테고리의 다른 글

[HackerRank]Breaking the Records  (0) 2019.07.08
[HackerRank]Apple and Orange  (0) 2019.07.03
[HackerRank]Time Conversion  (0) 2019.06.27
[HackerRank]Birthday Cake Candles  (0) 2019.06.26
[HackerRank]Mini-Max Sum  (0) 2019.06.25