[C++]백준 1436 영화감독 숌

칼든개구리·2025년 4월 21일
0
post-thumbnail

내가 작성한 코드

#include <bits/stdc++.h>
using namespace std;

int cnt, n;
int main()
{
    cin>>n;
    int i=666;
    while(true)
    {
        string a= to_string(i);
        if(a.find("666")!= string::npos){
            cnt++;
            if(n==cnt)
            {
                cout<<a<<'\n';
                break;
            }
        }
        i++;
    }
    return 0;
}

설명 포함 주석

#include <iostream>
#include <string>
using namespace std;

int main()
{
    int n;         // n번째로 "666"이 들어간 숫자를 찾기 위한 입력값
    cin >> n;      // 사용자로부터 입력 받기

    int cnt = 0;   // "666"이 들어간 숫자를 몇 개 찾았는지 세는 변수
    int i = 666;   // 666부터 시작해서 하나씩 증가시키며 검사

    while (true)
    {
        // 정수 i를 문자열로 변환
        string str = to_string(i);

        // 문자열 안에 "666"이 포함되어 있는지 확인
        if (str.find("666") != string::npos)
        {
            cnt++;  // 찾았으면 카운트 증가

            // n번째 숫자를 찾았다면 출력하고 종료
            if (cnt == n)
            {
                cout << str << '\n';
                break;
            }
        }

        i++; // 다음 숫자 검사
    }

    return 0;
}
profile
메타쏭이

0개의 댓글