본문 바로가기
알고리즘/코딩 테스트 문제

[프로그래머스] 등수매기기(C)

by 진진리 2023. 9. 21.
728x90
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// score_rows는 2차원 배열 score의 행 길이, score_cols는 2차원 배열 score의 열 길이입니다.
int* solution(int** score, size_t score_rows, size_t score_cols) {
    // return 값은 malloc 등 동적 할당을 사용해주세요. 할당 길이는 상황에 맞게 변경해주세요.
    int* answer = (int*)malloc(sizeof(int)*score_rows);
    double* avg = (double*)malloc(sizeof(double)*score_rows);
    for(int i=0;i<score_rows;i++){
        avg[i] = (score[i][0]+score[i][1])/2.0;
        answer[i] = 1;
    } 
    
    for(int i=0;i<score_rows;i++){
        double a = avg[i];
        for(int j=0;j<score_rows;j++){
            if(a>avg[j]) answer[j]++;
        }
    }

    return answer;
}

평균값을 저장하는 avg를 double형으로 한 뒤 나누기 2가 아닌 나누기 2.0으로 바꾼 뒤 통과

실수형 계산식 주의하기