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

 

프로그래머스

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

programmers.co.kr

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
 
int solution(long num) {
    int cnt = 0;
    for (cnt = 0; cnt < 500; cnt++)
    {
        if (num == 1)break;
        if (num % 2 == 0)
        {
            num /= 2;
        }
        else
        {
            num *= 3;
            num++;
        }
    }
    if (cnt == 500)
    {
        cnt = -1;
    }
    return cnt;
}

 

매개변수 int num 을 long num으로 바꾸지 않으면 테스트케이스3을 통과하지 못한다.

num 6226331이 범위를 벗어나기 때문이다.

 

+ Recent posts