재귀로 풀면 쉽게 풀린다.
using namespace std;
long long int COLLATZ(long long int n, int cnt)
{
if (cnt == 501)
return -1;
if (n == 1)
return cnt;
if (n % 2 == 0)
return COLLATZ(n / 2, cnt + 1);
else
return COLLATZ(n * 3 + 1, cnt + 1);
}
int solution(int num) {
int answer = 0;
answer = COLLATZ(num, 0);
return answer;
}'Coding_Test 연습 > Programmers' 카테고리의 다른 글
| [프로그래머스] (C++) LV1 하샤드 수 (0) | 2022.04.27 |
|---|---|
| [프로그래머스] (C++) LV1 평균 구하기 (0) | 2022.04.27 |
| [프로그래머스] (C++) LV1 최대공약수와 최소공배수 (0) | 2022.04.27 |
| [프로그래머스] (C++) LV1 짝수와 홀수 (0) | 2022.04.27 |
| [프로그래머스] (C++) LV1 제일 작은 수 제거하기 (0) | 2022.04.27 |