https://www.acmicpc.net/problem/2503
2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트
www.acmicpc.net
1에서 9까지의 서로 다른 숫자 세 개로 구성된 세 자리 수
각 턴 마다
123부터 999까지 다 돌아보는 것이다! -> 브루트포스 문제네
그러면서 범위를 줄여나간다! bool을 false로 바꿔서!
memset을 쓰려면 #include <cstring> 하는거 잊지말자
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
|
#include <iostream>
#include <vector>
#include <string>
#include <cstring>
#include <algorithm>
using namespace std;
vector<int> tri;
int n, guess, strike, ball;
bool arr[1000];
string tmp, one, two;
int strike_cnt, ball_cnt, ans;
int main()
{
memset(arr, true, sizeof(arr));
for (int i = 123; i <= 999; i++)
{
tmp = to_string(i);
if (tmp[0] == tmp[1] || tmp[0] == tmp[2] || tmp[1] == tmp[2])
arr[i] = false; //같은 숫자가 있으면 false 설정
if (tmp[0] == '0' || tmp[1] == '0' || tmp[2] == '0')
arr[i] = false;
}
cin >> n;
for (int i = 1; i <= n; i++)
{
cin >> guess >> strike >> ball;
for (int i = 123; i <= 999; i++)
{
strike_cnt = 0;
ball_cnt = 0;
if (arr[i])
{
one = to_string(guess);
two = to_string(i);
for (int x = 0; x < 3; x++)
{
for (int y = 0; y < 3; y++)
{
if (x == y && one[x] == two[y])
strike_cnt++;
if (x != y && one[x] == two[y])
ball_cnt++;
}
}
if (strike_cnt != strike || ball_cnt != ball)
arr[i] = false;
}
}
}
ans = 0;
for (int i = 123; i <= 999; i++)
{
if (arr[i] == true)
ans++;
}
cout << ans << '\n';
}
|
cs |