본문 바로가기
Computer Science/Coding Test

c++(cpp) 프로그래머스 : 코딩 테스트 - 콜라츠 추측

by hzyiunn 2022. 7. 27.
728x90
반응형

#include <string>
#include <vector>

using namespace std;

int solution(int n) {
    int answer = 0;
    long long num = n;
    while(num != 1)
    {
        if(answer < 500)
        {
            if(num % 2 == 0)
            {
                num = num / 2;
                answer ++;
            }
            else if(num % 2 != 0)
            {
                num = num * 3 + 1;
                answer ++;
            }     
        }
        else
            return -1;
    }
    return answer;
}
728x90
반응형

댓글