점수를 매기는 방법
- 40점 미만은 낙제다.
- 38점이상부터 반올림을 한다.
- 점수보다 높은 5의 배수와 3점 미만의 차이가 나면 반올림을 한다.
반올림 예) 38 -> 40, 58 -> 60
반올림 안하는 경우 : 55, 56, 77
코드
int* gradingStudents(int grades_count, int* grades, int* result_count) {
int *a = malloc(grades_count*sizeof(int));
*result_count = grades_count;
int i;
for(i=0; i<grades_count; i++){
a[i] = grades[i];
if(a[i]>=38){
if((a[i]/5+1)*5-a[i]<3){
a[i] = (a[i]/5+1)*5;
}
}
}
return a;
}
먼저 반올림한 성적을 저장할 a를 할당했고, a에 grades를 복사해줬다.
성적이 38이상이어야 반올림이 가능하기 때문에 if문으로 조건을 주었다.
그 성적의 다음 5의 배수를 (a[i]/5+1)*5같은 방식으로 계산해주었고, 이것과 원래 성적과의 차가 3이하이면 반올림해준다.
추가
a[i]%5 < 3 조건이 왜 안되지 생각해보니까 부등호가 >= 이어야되는데 착각했었다;;
int* gradingStudents(int grades_count, int* grades, int* result_count) {
int *a = malloc(grades_count*sizeof(int));
*result_count = grades_count;
int i;
for(i=0; i<grades_count; i++){
a[i] = grades[i];
if(a[i]>=38){
if(a[i]%5 >= 3){
a[i] = (a[i]/5+1)*5;
}
}
}
return a;
}
조건이 훨씬 더 깔끔하다.
'HackerRank > Algorithms' 카테고리의 다른 글
[HackerRank C] Strings : HackerRank in a String! (0) | 2021.07.11 |
---|---|
[HackerRank C] Strings : CamelCase (0) | 2021.07.11 |
[HackerRank C] Sorting : Counting Sort 1 (0) | 2021.05.23 |
[HackerRank C] Sorting : Quicksort 1 - Partition (0) | 2021.05.16 |
[HackerRank] Sorting : Find the Median (0) | 2021.05.09 |