Programmers/Level1
콜라츠 추측
zzunsik
2021. 7. 7. 13:12
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
class Solution {
public int solution(int num) {
long lNum = num;
int answer = 0;
while (lNum != 1) {
if (lNum % 2 == 0) {
lNum /= 2;
} else {
lNum = lNum * 3 + 1;
}
answer++;
if(answer == 500) {
answer = -1;
break;
}
}
return answer;
}
}
|
cs |
num에 3을 곱할 때 int의 범위를 넘어가는 경우가 생긴다.
그러므로 long으로 해결해야 한다.