[알고리즘] (프로그래머스) 콜라츠 추측

이용찬·2022년 1월 11일
0

알고리즘

목록 보기
24/31
post-thumbnail

문제
(프로그래머스) 콜라츠 추측

Java 풀이

class Solution {
    public int solution(int num) {
        long temp = num;
        int answer = 0;
        
        while(temp != 1) {
            if(temp % 2 == 0) {
                temp /= 2;
            } 
            else {
                temp = (temp * 3) + 1;
            }
            answer++;
            
            if(answer == 500) {
                return -1;
            }
        }
        return answer;
    }
}
profile
안녕하세요. 클래식을 즐기는 개발자, 이용찬입니다.

0개의 댓글