본문 바로가기
알고리즘/프로그래머스 1단계

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

by lanuarius19 2022. 4. 23.
728x90

 

프로그래머스 1단계
콜라츠 추측

https://programmers.co.kr/learn/challenges

 

문제 설명

 

콜라츠 추측

 

 

 

풀이

 

class Solution {
  public int solution(double num) {
      int answer = 0;
      if(num == 1) {
          return 0;
      }
      
      while(num != 1) {
          if(answer == 500) {
              return -1;
          }
          if(num % 2 == 0) {
              num /= 2;
              answer++;
          } else {
              num = num * 3 + 1;
              answer++;
          }
          System.out.print(num+" ");
      }  
      return answer;
  }
}
 
728x90

댓글