글
프로그래머스 C# 콜라츠 추측(while, long, 1일 때 예외처리) 쉬움
프로그래밍
2023. 1. 23. 16:30
728x90
SMALL
public class Solution {
public int solution(long num) {
int a = 0;
if(num == 1)
{
return 0;
}
while(a<500)
{
a++;
if(num%2 == 0)
{
num /= 2;
}
else
{
num = num * 3 + 1;
}
if(num == 1)
{
return a;
}
else if(a == 500)
{
return -1;
}
}
return a;
}
}
728x90
'프로그래밍' 카테고리의 다른 글
프로그래머스 C# 피보나치 수(점화식) (0) | 2023.01.23 |
---|---|
프로그래머스 C# 하샤드 수(0 예외 처리) (0) | 2023.01.23 |
프로그래머스 C# 최대공약수와 최소공배수(for문) (0) | 2023.01.23 |
C# 최댓값과 최솟값(Split().Select(x => ), OrderBy, ToList()) (0) | 2023.01.23 |
프로그래머스 C# 제일 작은 수 제거하기(array.where) (0) | 2023.01.23 |