본문 바로가기

코딩테스트/C++

백준 브루트 포스 알고리즘 1436번 C++

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

int find(int N) {
	int i = 666;
	string target;
	int count = 0;
	while (1) {
		target = to_string(i);
		for (int j = 0; j < target.length()-2; j++) { //j<target.length()-2를 생각하기가 오래걸렸다.
			if (target[j] == '6' && target[j + 1] == '6' && target[j + 2] == '6') {
				count++;
				if (count == N)
					return i;
				break; //이게 제일 중요한 것 같다. 6666일때 2번 카운트가 되기 때문이다.	
			}
		}
		i++;
	}
}

int main() {
	
	ios::sync_with_stdio(false);
	cin.tie(NULL);
	cout.tie(NULL);

	int N;
	cin >> N;
	cout << find(N);
}

string을 int로 변경하는 stoi 함수

int를 string으로 변경하는 to_string 함수