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
'알고리즘 > 프로그래머스 1단계' 카테고리의 다른 글
[프로그래머스] 최대공약수와 최소공배수 (0) | 2022.04.24 |
---|---|
[프로그래머스] K번째 수 (0) | 2022.04.24 |
[프로그래머스] 짝수와 홀수 (0) | 2022.04.23 |
[프로그래머스] x만큼 간격이 있는 n개의 숫자 (0) | 2022.04.23 |
[프로그래머스] 평균 구하기 (0) | 2022.04.23 |
댓글