728x90
Practice > Algorithms > Implementation > Grading Students
문제
학생의 점수를 받고, 성적을 낸다.
점수의 다음 5의 배수와 3미만의 차이가 날 경우 다음 5의 배수로 점수를 올린다.
하지만 38점 보다 작으면 점수를 올리지 않고 유지한다.
Input Format
int n : the number of students
line i : grades[i]
풀이
매개변수: int grades_count, int* grades, int* result_count
grades_count 학생 수, grades 학생 점수가 들어있는 배열
리턴할 *result를 grades_count(학생 수) 크기만큼 할당.
if (38점 이상이면) { 최종 점수 조정 }
else { 38점 미만이면 점수 유지 }
printf ( 조정된 최종 점수 출력 )
코드
int* gradingStudents(int grades_count, int* grades, int* result_count) {
int *result = malloc(grades_count*sizeof(int)); // return할 결과 값 동적 할당
(*result_count) = 0;
for(int i=0; i<grades_count; i++){
if(grades[i]>=38){ // 38점 이상 시
if(5-(grades[i]%5) == 1){ // 5의 배수와 1 차이 나면 반올림
result[i] = grades[i] + 1;
}
else if(5-(grades[i]%5) == 2){ // 5의 배수와 2 차이 나면 반올림
result[i] = grades[i] + 2;
}
else{
result[i] = grades[i];
}
}
else{ // 38점 미만
result[i] = grades[i];
}
(*result_count)++;
printf("%d\n",result[i]);
}
return result;
}
이렇게도 바꿔봤다
if(grades[i]>=38){
if(grades[i]%5 > 2)
result[i] = grades[i] + 5 - (grades[i] % 5);
else{
result[i] = grades[i];
}
}
결과
728x90
반응형
'문제풀이 > C' 카테고리의 다른 글
[Hackerrank] Library Fine (0) | 2020.11.08 |
---|---|
[Hackerrank] Utopian Tree (0) | 2020.10.11 |
[Hackerrank] Sherlock and Squares (0) | 2020.10.11 |
[Hackerrank] Divisible Sum Pairs (0) | 2020.10.04 |
[Hackerrank] 3D Surface Area (0) | 2020.09.20 |