문제풀이:
#include <string>
using namespace std;
int solution(int num){
long long n = num;
int answer = 0;
while(true){
if(n==1)
break;
n%2 == 0 ? n/=2 : n = 3*n + 1;
answer++;
if(answer==500){
answer=-1;
break;
}
}
return answer;
}
'코딩테스트' 카테고리의 다른 글
프로그래머스_0단계_배열의 길이에 따라 다른 연산하기 C++ (0) | 2024.04.25 |
---|---|
프로그래머스_0단계_인덱스 바꾸기 C++ (0) | 2024.04.25 |
프로그래머스_1단계_약수의 합 C++ (0) | 2024.04.23 |
프로그래머스_0단계_첫 번째로 나오는 음수 C++ (0) | 2024.04.23 |
프로그래머스_0단계_대문자와 소문자 C++ (0) | 2024.04.23 |