반응형

문제 설명

자연수 n을 뒤집어 각 자리 숫자를 원소로 가지는 배열 형태로 리턴해주세요. 예를들어 n이 12345이면 [5,4,3,2,1]을 리턴합니다.

 

제한 조건

  • n은 10,000,000,000이하인 자연수입니다.

입출력 예

n return
12345 [5,4,3,2,1]

solution.c

// 리턴할 값은 메모리를 동적 할당해주세요.
static int *first_digit_array;

int GetFirstDigitCount(long long n)
{
    int count = 0;

    while (n)
    {
        count++;
        n /= 10;
    }

    return count;
}

int *GetFirstDigit(long long n)
{
    first_digit_array = (int *)malloc(GetFirstDigitCount(n) * sizeof(int));
    int count = 0;

    while (n)
    {
        first_digit_array[count++] = n % 10;
        n /= 10;
    }

    return first_digit_array;
}

int *solution(long long n)
{
    return GetFirstDigit(n);
}

 

반응형

+ Recent posts