코딩테스트

[백준] 1436 영화감독 숌

yeoul0714 2025. 5. 30. 02:21
#include "bits/stdc++.h"

using namespace std;

bool Check(int input)
{
    string strInput= to_string(input);

    int cnt = 0;

    for (char ch : strInput)
    {
        if (ch == '6')
        {
            cnt++;
        }
        else
        {
            cnt = 0;
        }

        if (cnt == 3)
        {
            return true;
        }

    }

    return false;
}

int main() {
 
    int n;

    cin >> n;

    int result= 666;

    int order = 0;

    while(true)
    {
        if (Check(result))
        {
            order++;
        }

        if (order == n)
            break;

        result++;
    }

    cout << result;


    return 0;
}

 

단순히 브루트포스 알고리즘으로 숫자를 하나씩 늘려가며 그것이 종말의 수인지를 판별하는 방식으로 구현하였다.

 

브루트포스로 안되는 경우엔 추가적인 알고리즘을 생각해내야 하지만 이 문제의 경우에는 N의 범위가 10000까지밖에 안되는 등

 

브루트포스로 풀만한 문제였다.

 

find함수를 써서 string::npos랑 비교하는 방식으로 풀었으면 좀더 간단하게 풀 수 있었다.

 

이부분은 잘 몰랐던 부분이므로 잘 기억해두도록 하자

bool Check(int input)
{
    string strInput = to_string(input);
    
    // "666" 문자열이 포함되어 있는지 확인
    if (strInput.find("666") != string::npos)
    {
        return true;
    }
    
    return false;
}

'코딩테스트' 카테고리의 다른 글

[백준] 4949 균형잡힌 세상  (0) 2025.07.01
[백준] 9012 괄호  (0) 2025.05.30
[백준] 2852 NBA 농구  (0) 2025.05.17
[백준] 3474번 교수가 된 현우  (2) 2025.04.30
[백준] 10709번 기상캐스터  (0) 2025.04.30