-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLevel2Collatz.java
More file actions
46 lines (43 loc) · 1.25 KB
/
Copy pathLevel2Collatz.java
File metadata and controls
46 lines (43 loc) · 1.25 KB
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package programmers;
public class Level2Collatz {
public int solution(int num) {
//입력값을 long으로 해야하는게 맞음
//3번째 케이스에서 626331일 때 int에 저장할 수 있는 값의 범위를 넘어서게 되어서 488을 리턴하는 것임
int answer = 0;
if (num == 1) {
return 0;
}
while (num != 1) {
if (num % 2 == 0) {
num = num / 2;
} else {
num = (num * 3) + 1;
}
answer++;
if (answer == 500) {
answer = -1;
break;
}
}
return answer;
}
// public int solution(int num) { //
// int answer = 0;
// if (num == 1) {
// return 0;
// }
// while (num != 1) {
// if (num % 2 == 0) {
// num = num / 2;
// } else if(num % 2 ==1){ //else로 하면 488 , else if로 하면 답나옴..
// num = (num * 3) + 1;
// }
// answer++;
// }
// return answer;
// }
public static void main(String[] args) {
Level2Collatz a = new Level2Collatz();
System.out.println(a.solution(626331));
}
}