Algorithm/PROGRAMMERS[Java]
Lv1. 콜라츠 추측
씨니
2021. 9. 10. 20:24
728x90
프로그래머스 7번째 문제!
class Solution {
public int solution(double num) {
int answer = 0;
while(num > 1){
if(num % 2 == 0){
num /= 2;
}else{
num = (num * 3) + 1;
}
answer++;
}
if(answer > 500){
answer = -1;
}
return answer;
}
}
728x90