728x90
반응형
https://www.acmicpc.net/problem/1436
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
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
반응형
'알고리즘 > 완전탐색' 카테고리의 다른 글
[java 백준]골드 4/ 1963번 소수경로 (0) | 2022.02.26 |
---|---|
[java 백준]실버 1/1697번 숨바꼭질 (0) | 2022.02.23 |
[java 백준] 골드 1/ 2098번 외판원 순회 2 (0) | 2022.02.20 |
[java 백준] 골드 5/1451번 직사각형으로 나누기 (0) | 2022.02.17 |
[java 백준] 골드 5/1107번 리모컨 (0) | 2022.02.13 |
댓글