본문 바로가기
ⓒⓞⓓⓘⓝⓖⓣⓔⓢⓣ/ⓒ

[프로그래머스/C언어] 콜라츠 추측

by heaven00 2023. 11. 9.
728x90

📌 문제 링크

https://school.programmers.co.kr/learn/courses/30/lessons/12943

 

프로그래머스

코드 중심의 개발자 채용. 스택 기반의 포지션 매칭. 프로그래머스의 개발자 맞춤형 프로필을 등록하고, 나와 기술 궁합이 잘 맞는 기업들을 매칭 받으세요.

programmers.co.kr

 

 

계속 왜 틀렸지 ~ 했는데 범위를 바꿔주니 바로 정답 된 문제

 

정수형 메모리 크기 데이터 표현 범위
char 1바이트 (8비트) - 128 ~ + 127
short 2바이트 (16비트) -32768 ~ + 32767
int 4바이트 (32비트) -2147483648 ~ + 2147483647
long 4바이트 (32비트) -2147483648 ~ + 2147483647

 

실수형 메모리 크기 데이터 표현 범위
float 4바이트 (32비트) 1.17*10^-38 ~ 3.40*10^38
double 8바이트 (64비트) 2.22*10^-308 ~ 1.79*10^308
long double 8바이트 (64비트) 2.22*10^-308 ~ 1.79*10^308

 

 

👩‍💻 전체코드

#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>

int solution(long long num) {
    int answer = 0;
    int count = 0;
    while(num != 1) {
        if(count < 499) {
            if(num % 2 == 0) {
                num /= 2;
            } else {
                num = num * 3 + 1;
            }
            count += 1;
        } else {
            return -1;
        }
    }
    return count;
}
728x90

댓글