[BOJ] C++ 1436 "영화감독 숌" 문제 풀이 _ nov

반응형
반응형

#INFO

난이도 : SILVER3

문제유형 : 브루트포스 알고리즘

출처 : https://www.acmicpc.net/problem/1436


#SOLVE

모든 경우의 수를 탐색하는 브루트포스 방식으로 문제를 풀이하였다.

탐색을 수행할 숫자(cnt)to_string 함수를 이용해 문자열 타입으로 바꿔준 뒤, find 함수를 사용해 cnt 내부에 연속된 666이 존재하는지 탐색한다.

		string str = to_string(cnt);
		// cnt에서 연속된 666을 탐색한 경우
		if(str.find("666") != -1){
			a++;
		}

#CODE

#include <iostream>
#include <string> // to_string, find
using namespace std;

void solve(int n){
	ios::sync_with_stdio(0);
	cin.tie(0); cout.tie(0);
	int a = 0;
	int cnt = 0;
	while(true){
		string str = to_string(cnt);
		// cnt에서 연속된 666을 탐색한 경우
		if(str.find("666") != -1){
			a++;
		}
		if(a == n){
			cout << cnt << '\n';
			break;
		}
		cnt++;
	}
}

int main(){
	int n;
	cin >> n;
	solve(n);
	return 0;
}
반응형

댓글

Designed by JB FACTORY