반응형
문제 설명
자연수 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);
}
반응형
'공부 > 프로그래머스' 카테고리의 다른 글
[프로그래머스] 정수 제곱근 판별 (0) | 2023.03.05 |
---|---|
[프로그래머스] 나머지가 1이 되는 수 찾기 (0) | 2023.03.04 |
[프로그래머스] 약수의 합 (0) | 2023.03.03 |
[프로그래머스] 평균 구하기 (0) | 2023.03.03 |
[프로그래머스] 자릿수 더하기 (0) | 2023.03.03 |