문제) 백준 - 구현 - 숫자 야구
-> www.acmicpc.net/problem/2503
2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트
www.acmicpc.net
분명 파이썬으로 풀 때는 쉬웠는데... 막상 C++로 구현하려니 어려웠던 문제.
구현 능력을 간과하지 말자.
소스 코드)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <bits/stdc++.h> | |
#define endl "\n" | |
using namespace std; | |
int n; | |
int numbers[101], strike[101], ball[101]; | |
bool IsNumberOkay(int number) { | |
vector<int> nb(3); | |
for (int i = 0; i < 3; ++i) { | |
nb[i] = number % 10; | |
if (nb[i] == 0) return false; | |
number /= 10; | |
} | |
for (int i = 0; i < 3; ++i) | |
for (int j = 0; j < 3; ++j) | |
if (i != j && nb[i] == nb[j]) | |
return false; | |
return true; | |
} | |
int count_Strike(int number, int target) { | |
int cnt = 0; | |
for (int i = 0; i < 3; ++i) { | |
if (number % 10 == target % 10) | |
cnt++; | |
number /= 10; target /= 10; | |
} | |
return cnt; | |
} | |
int count_Ball(int number, int target) { | |
vector<int> nb(3); | |
vector<int> tg(3); | |
int cnt = 0; | |
for (int i = 0; i < 3; ++i) { | |
nb[i] = number % 10; | |
tg[i] = target % 10; | |
number /= 10; target /= 10; | |
} | |
for (int i = 0; i < 3; ++i) | |
for (int j = 0; j < 3; ++j) { | |
if (i != j && nb[i] == tg[j]) | |
cnt++; | |
} | |
return cnt; | |
} | |
int main() { | |
cin >> n; | |
for (int i = 0; i < n; ++i) { | |
cin >> numbers[i] >> strike[i] >> ball[i]; | |
} | |
int res = 0, cnt = 0; | |
for (int i = 123; i < 1000; ++i) { | |
if (!IsNumberOkay(i)) continue; | |
cnt = 0; | |
for (int j = 0; j < n; ++j) { | |
if (strike[j] == count_Strike(i, numbers[j]) && ball[j] == count_Ball(i, numbers[j])) | |
cnt++; | |
} | |
if (cnt == n) res++; | |
} | |
cout << res << endl; | |
} |
반응형
'PS(Problem Solving) > 백준_BOJ' 카테고리의 다른 글
[백준] 1991번 - 트리 순회 (C++/파이썬) (0) | 2021.04.14 |
---|---|
[백준] 2437번 - 저울 (C++/파이썬) (0) | 2021.04.13 |
[백준] 2579번 - 계단 오르기(C++/파이썬) 문제 및 풀이 (0) | 2021.04.09 |
[백준] 1629번 - 곱셈 (C++) 문제 및 풀이 (0) | 2021.04.09 |
[백준] 2217번 - 로프 (파이썬/C++) 문제 및 풀이 (0) | 2021.04.05 |
댓글