Programming/C C++

Breaking the Records

728x90

https://www.hackerrank.com/challenges/breaking-best-and-worst-records/problem

문제는 쉬운 문제인데 함수의 리턴 타입을 고려해서 풀어야하는 문제이다.

int* breakingRecords(int scores_count, int* scores, int* result_count) {
    int *breaking=malloc(sizeof(int)*2);
    breaking[0]=0;  breaking[1]=0;
    int min=scores[0], max=scores[0];
    for(int i=1;i<scores_count;i++){
        if(min>scores[i])   {
            breaking[1]++; 
            min=scores[i];
        }
        if(max<scores[i])
        {
            breaking[0]++;
            max=scores[i];
        }   
    }
    *result_count = 2;
    return breaking;
}

△ 작성한 부분

breakingRecords 함수의 return-type은 int* type으로 리턴값으로 전달할 배열인 breaking 의 자료형을 주의해서 코드를 작성해야 한다.

처음에는 int *breaking[2] = {0,0}; 으로 했었는데 오류가 발생했다. 이 때에는 의미가 달라져서 *breaking[0] = 0, *breaking[1] = 0이 된 것 같다. 따라서 breaking 배열을 포인터타입으로 선언하고 malloc으로 동적할당해 배열로 활용한다.

#include <assert.h>
#include <limits.h>
#include <math.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char* readline();
char** split_string(char*);

int* breakingRecords(int scores_count, int* scores, int* result_count) {
    int *breaking=malloc(sizeof(int)*2);
    breaking[0]=0;  breaking[1]=0;
    int min=scores[0], max=scores[0];
    for(int i=1;i<scores_count;i++){
        if(min>scores[i])   {
            breaking[1]++; 
            min=scores[i];
        }
        if(max<scores[i])
        {
            breaking[0]++;
            max=scores[i];
        }   
    }
    *result_count = 2;
    return breaking;
}

int main()
{
    FILE* fptr = fopen(getenv("OUTPUT_PATH"), "w");

    char* n_endptr;
    char* n_str = readline();
    int n = strtol(n_str, &n_endptr, 10);

    if (n_endptr == n_str || *n_endptr != '\0') { exit(EXIT_FAILURE); }

    char** scores_temp = split_string(readline());

    int* scores = malloc(n * sizeof(int));

    for (int i = 0; i < n; i++) {
        char* scores_item_endptr;
        char* scores_item_str = *(scores_temp + i);
        int scores_item = strtol(scores_item_str, &scores_item_endptr, 10);

        if (scores_item_endptr == scores_item_str || *scores_item_endptr != '\0') { exit(EXIT_FAILURE); }

        *(scores + i) = scores_item;
    }

    int scores_count = n;

    int result_count;
    int* result = breakingRecords(scores_count, scores, &result_count);

    for (int i = 0; i < result_count; i++) {
        fprintf(fptr, "%d", *(result + i));

        if (i != result_count - 1) {
            fprintf(fptr, " ");
        }
    }

    fprintf(fptr, "\n");

    fclose(fptr);

    return 0;
}

char* readline() {
    size_t alloc_length = 1024;
    size_t data_length = 0;
    char* data = malloc(alloc_length);

    while (true) {
        char* cursor = data + data_length;
        char* line = fgets(cursor, alloc_length - data_length, stdin);

        if (!line) {
            break;
        }

        data_length += strlen(cursor);

        if (data_length < alloc_length - 1 || data[data_length - 1] == '\n') {
            break;
        }

        alloc_length <<= 1;

        data = realloc(data, alloc_length);

        if (!line) {
            break;
        }
    }

    if (data[data_length - 1] == '\n') {
        data[data_length - 1] = '\0';

        data = realloc(data, data_length);
    } else {
        data = realloc(data, data_length + 1);

        data[data_length] = '\0';
    }

    return data;
}

char** split_string(char* str) {
    char** splits = NULL;
    char* token = strtok(str, " ");

    int spaces = 0;

    while (token) {
        splits = realloc(splits, sizeof(char*) * ++spaces);

        if (!splits) {
            return splits;
        }

        splits[spaces - 1] = token;

        token = strtok(NULL, " ");
    }

    return splits;
}

 

 

 

 

 

 

 

 

SMALL

'Programming > C C++' 카테고리의 다른 글

Birthday Chocolate  (0) 2020.08.30
[DS] Insert a Node at the Tail of a Linked List  (0) 2020.08.23
Print the Elements of a Linked List  (0) 2020.08.16
Kangaroo  (0) 2020.08.16
Find the Merge point of two joined linked lists  (0) 2020.08.09