본문 바로가기
알고리즘/완전탐색

[C++ 백준] 실버 5/1436번 영화감독 숌

by Meaning_ 2022. 2. 21.
728x90
반응형

 

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

 

1436번: 영화감독 숌

666은 종말을 나타내는 숫자라고 한다. 따라서, 많은 블록버스터 영화에서는 666이 들어간 제목을 많이 사용한다. 영화감독 숌은 세상의 종말 이라는 시리즈 영화의 감독이다. 조지 루카스는 스타

www.acmicpc.net

 

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
26
27
28
29
30
31
32
#include <iostream>
#include<string>
#include<istream>
 
#include<algorithm>
 
using namespace std;
 
 
int main() {
    int n;
    scanf("%d"&n);
 
    int num = 666;
    string s = "";
 
    int count = 1;
    
    while (count != n) {
        num++;
 
        s = to_string(num);
        if (s.find("666"!= std::string::npos) {
            count++;
        }
 
            
 
    }
 
    printf("%d\n", num);
}
cs

 

처음엔 좀 어렵게 생각했는데 그냥 숫자를 문자열로 바꿔서 문자열에 666이 있으면 count를 늘려주면 되었다.

 

1번째 이면 666

2번째 이면 1666 3번째 이면 2666 4번째 이면 366 65번째 이면 4666 6번째 이면 5666 7번째 이면 6660이다. 

 

왜 7번째는 7666이 안되나? 라는 생각이 들 수도 있는데 6660이 7666보다 작으니까 6660이 차례대로 라는 조건을 만족시키게 된다.

 

 

이때 사용한건 find라는 함수인데

https://sweetnew.tistory.com/85

 

[C++] 문자열 찾기: string.find();

size_t find(const string& str, size_T pos = 0) const; str : 찾고자 하는 문자(열) pos: 찾기 시작하는 주솟값 ​ string.find 함수는 헤더 파일에 정의되어 있으며, 찾고자 하는 문자(열) str을 찾아준다. 그..

sweetnew.tistory.com

 

string 헤더 파일에 있으며 찾고자 하는 문자열을 찾아준다.

 

 

잘못 생각한 코드 

 

처음에는

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include<string>
#include<istream>
 
#include<algorithm>
 
using namespace std;
 
 
int main() {
    int n;
    cin >> n;
 
    int num = 666;
    string s = "";
 
    int count = 1;
    
    while (count != n) {
        num++;
 
        s = to_string(num);
        int len = s.length();
        int cnt = 0;
        for (int i = 0; i < len; i++) {
            
            if (s[i]=='6') {
                cnt++;
            }
        }
        if (cnt >= 3) {
            count++;
 
        }
        
 
        if (count == n) {
            break;
        }
        
 
        
 
        
    }
 
    cout << num << endl;
 
 
}
 
cs

 

이렇게 썼는데 이러면 6066,6616,6676 이런 경우도 경우의 수에 포함시켜버렸다. 한마디로 666은 셋이 같이 있어야지 어느것 하나라도 떨어져 있으면 안됐던 것이다.

 

728x90
반응형

댓글