콜라츠 추측
https://programmers.co.kr/learn/courses/30/lessons/12943
코딩테스트 연습 - 콜라츠 추측
1937년 Collatz란 사람에 의해 제기된 이 추측은, 주어진 수가 1이 될때까지 다음 작업을 반복하면, 모든 수를 1로 만들 수 있다는 추측입니다. 작업은 다음과 같습니다. 1-1. 입력된 수가 짝수라면 2
programmers.co.kr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string> | |
#include <vector> | |
using namespace std; | |
int solution(int num) { | |
long long data = num; | |
int count = 0; | |
while(data != 1) | |
{ | |
if (data % 2 == 0) | |
{ | |
data /= 2; | |
} | |
else | |
{ | |
data = (data * 3) + 1; | |
} | |
++count; | |
if (500 <= count) | |
{ | |
return -1; | |
} | |
} | |
return count; | |
} |
반응형
'자료구조 와 알고리즘 > 프로그래머스' 카테고리의 다른 글
이상한 문자 만들기 (0) | 2022.05.08 |
---|---|
자릿수 더하기 (0) | 2022.05.08 |
평균 구하기 (0) | 2022.05.08 |
하샤드 수 (0) | 2022.05.08 |
핸드폰 번호 가리기 (0) | 2022.05.08 |