Algorithm/Programers - C++

[프로그래머스] 콜라츠 추측

StrongMin 2022. 2. 27. 23:29

문제

풀이

#include <string>
#include <vector>

using namespace std;

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