Skip to content

Instantly share code, notes, and snippets.

@DongguemYoo
Created May 3, 2020 10:16
Show Gist options
  • Save DongguemYoo/2f3b3755b67ef069e33e3199db41ae55 to your computer and use it in GitHub Desktop.
Save DongguemYoo/2f3b3755b67ef069e33e3199db41ae55 to your computer and use it in GitHub Desktop.
코딩테스트 연습 콜라츠 추측
public int solution(int num)
{
int answer = 0;
while (num != 1)
{
if (num % 2 == 0)
{
num /= 2;
}
else if (num % 2 == 1)
{
num = (num *= 3) + 1;
}
if (answer == 500)
{
answer = -1;
break;
}
answer++;
}
return answer;
}
//다른사람이 푼 풀이
//겁나 깔
public class Solution {
public int solution(int num) {
long lNum = num;
for (int i = 0; i < 500; i++)
{
if (lNum == 1) return i;
lNum = lNum % 2 == 0 ? lNum / 2 : lNum * 3 + 1;
}
return -1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment