본문 바로가기

문제풀이/C 문제풀이

[HackerRank]Breaking the Records

문제

Maria plays college basketball and wants to go pro. Each season she maintains a record of her play. She tabulates the number of times she breaks her season record for most points and least points in a game. Points scored in the first game establish her record for the season, and she begins counting from there.

Function Description
Complete the breakingRecords function in the editor below. It must return an integer array containing the numbers of times she broke her records. Index 0 is for breaking most points records, and index 1 is for breaking least points records.
Input Format
The first line contains an integer n, the number of games.
The second line contains n space-separated integers describing the respective values of socre0, score1, ... , scoren-1.
Output Format
Print two space-seperated integers describing the respective numbers of times her best (highest) score increased and her worst (lowest) score decreased.

CODE

int* breakingRecords(int scores_count, int* scores, int* result_count) {
    int min = pow(10, 8)+1, max = -1; //최솟값, 최댓값 사전 설정
    int *re = malloc(2*sizeof(int)); re[0] = re[1] = -1; // return할 포인터, 사전 값은 -1로 해야 첫 max, min 값 입력 후에 0으로 초기화 할 수 있다.
    for(int i=0;i<scores_count;i++){
        if(scores[i] > max) { //최대일 때
            max = scores[i];
            re[0]++;
        }
        if(scores[i] < min) { // 최소일 때
            min = scores[i];
            re[1]++;
        }
    }
    *result_count = 2; // main의 코드를 보면 re의 element의 개수를 입력해야 함
    return re;
}

감상

이거보다 간단한 코드가 있을텐데.
malloc으로 설정해야 코드 실행 시 오류가 발생하지 않는다

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

[HackerRank]Between Two Sets  (0) 2019.07.30
[HackerRank]Kangaroo  (0) 2019.07.11
[HackerRank]Apple and Orange  (0) 2019.07.03
[HackerRank]Grading Students  (0) 2019.07.01
[HackerRank]Time Conversion  (0) 2019.06.27