완전 탐색 (Brute Force)
백준 2503 숫자 야구
ssam..
2020. 4. 24. 03:41
문제 링크: https://www.acmicpc.net/problem/2503
2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트라이크 개수를 나타내는 정수와 볼의 개수를 나타내는 정수, 이렇게 총 세 개의 정수가 빈칸을 사이에 두고 주어진다.
www.acmicpc.net
각 질문마다 100에서 999중에 가능하지 않은 숫자들을 제거해나가고, 마지막에 남는 숫자들의 갯수를 세면 된다.
100에서 999중에 0이 들어가거나, 숫자가 중복되는경우도 잘 제거해줘야 한다.
to_string 함수를 사용해서 숫자를 문자열로 바꿨는데도 불구하고, 0이 들어가는 숫자들을 제거해줄때 0을 숫자취급해서 여러번 틀렸는데 '0'으로 고치고나서 AC를 받았다.
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
|
#include <bits/stdc++.h>
using namespace std;
int a[1000];
bool possible(int num1, int num2, int strike, int ball) {
string s1 = to_string(num1);
string s2 = to_string(num2);
if(s1[0] == '0' || s1[1] == '0' || s1[2] == '0') return false;
if(s1[0] == s1[1] || s1[0] == s1[2] || s1[1] == s1[2]) return false;
int s = 0;
int b = 0;
for(int i = 0; i < 3; i++) {
for(int j = 0; j < 3; j++) {
if(s1[i] == s2[j]) {
if(i == j) s++;
else b++;
}
}
}
if(s == strike && b == ball) return true;
return false;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
for(int i = 100; i <= 999; i++) a[i] = 1;
int n;
cin >> n;
for(int i = 0; i < n; i++) {
int x, s, b;
cin >> x >> s >> b;
for(int j = 100; j <= 999; j++) {
if(!possible(j, x, s, b))
a[j] = 0;
}
}
int ans = 0;
for(int i = 100; i <= 999; i++)
ans += a[i];
cout << ans << '\n';
return 0;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|