본문 바로가기
공부

(프로그래머스)없는 숫자 더하기

by 하프상 2021. 10. 6.
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

// numbers_len은 배열 numbers의 길이입니다.
int solution(int numbers[], size_t numbers_len) {
    int answer = 0;
    int check[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    
    int *result = (int*)malloc(sizeof(int)*numbers_len); // 정수 배열 동적 할당
    
    for(int i=0; i<numbers_len; i++)
    {
        result[i] = numbers[i];
    }
    
    for(int i=0; i<numbers_len; i++)
    {
        for(int j=0; j<10; j++)
        {
            if(result[i] == check[j])
            {
                check[j] = 0;
            }
        }
    }
    
    for(int i=0; i<10; i++)
    {
        if(check[i] != 0)
        {
            answer += check[i];
        }
    }
    
    return answer;
}

 

*******************************************************************************************************

동적 할당 하고나서 입력으로 주어지는 배열 값을

동적 할당 한 배열에 넣지 않아서 뭐가 문제인지 한참 찾았었다..

이상한 실수가 넘 많네.

*******************************************************************************************************

댓글