웅재의 코딩세상
[프로그래머스] 콜라츠 추측 본문
- 풀이
#include <string>
#include <vector>
using namespace std;
int solution(int num) {
long long n = num;
int answer = 0;
while(1){
if(n==1) break;
n%2 == 0 ? n/=2 : n = n*3 + 1;
answer++;
if(answer==500){
answer=-1;
break;
}
}
return answer;
}
//제한에서 8000000미만의 정수라고 하였으므로 크기 제한때문에 int형에서 long long 형으로 변환시켜줘야 한다.
반응형
'코딩테스트 > 프로그래머스 - LV 1' 카테고리의 다른 글
[프로그래머스] 짝수와 홀수 (0) | 2024.02.09 |
---|---|
[프로그래머스] 최대공약수와 최소공배수 (0) | 2024.02.09 |
[프로그래머스] 평균 구하기 (0) | 2024.02.09 |
[프로그래머스] 하샤드 수 (0) | 2024.02.09 |
[프로그래머스] 핸드폰 번호 가리기 (0) | 2024.02.09 |