Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

코딩응급실

프로그래머스: 콜라츠 추측 본문

Java

프로그래머스: 콜라츠 추측

Daeryuk Kim 2024. 1. 21. 18:59
class Solution {
    public int solution(int number) {
        long num = number; // 이거 없어서 계속 틀렸음... long으로 해야 큰 수일 때도 처리가능
        int ans = 0;
        
        while (num != 1) {
            if (num % 2 == 0) {
                num /= 2;
            } else {
                num = num * 3 + 1;
            }
            ans++;
            if (ans == 500) {
                return -1;
            }
        }
        
        return ans;
    }

    public static void main(String[] args) {
        int n = 626331;

        Solution sol = new Solution();
        int result = sol.solution(n);
        
        System.out.println(result);
    }
}