1-4. 프로그래머스
[프로그래머스] 레벨 2 - 숫자 야구
asdklfjlasdlfkj
2020. 2. 6. 23:02
https://programmers.co.kr/learn/courses/30/lessons/42841
코딩테스트 연습 - 숫자 야구 | 프로그래머스
[[123, 1, 1], [356, 1, 0], [327, 2, 0], [489, 0, 1]] 2
programmers.co.kr
완전 탐색 유형이라고는 하지만 for문 네 개면 풀리는 싱거운 문제다.
11번줄의 사항을 넣지 않으면 실패하는 테스트케이스가 존재하므로 꼭 넣어줘야 한다.
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
|
#include <string>
#include <vector>
using namespace std;
int solution(vector<vector<int> > baseball) {
int answer = 0;
for(int a=1; a<=9; a++){
for(int b=1; b<=9; b++){
for(int c=1; c<=9; c++){
if(a == b || a == c || b == c) continue;
bool check = true;
for(int i=0; i<baseball.size(); i++){
int strike = 0;
int ball = 0;
int aa = baseball[i][0]/100;
int bb = (baseball[i][0] - aa*100) / 10;
int cc = baseball[i][0] % 10;
int t_strike = baseball[i][1];
int t_ball = baseball[i][2];
if(a == aa) strike++;
if(b == bb) strike++;
if(c == cc) strike++;
if(a == bb || a == cc) ball++;
if(b == aa || b == cc) ball++;
if(c == aa || c == bb) ball++;
if(strike != t_strike || ball != t_ball) {
check = false;
break;
}
}
if(check) answer++;
}
}
}
return answer;
}
http://colorscripter.com/info#e" target="_blank" style="color:#4f4f4ftext-decoration:none">Colored by Color Scripter
|
http://colorscripter.com/info#e" target="_blank" style="text-decoration:none;color:white">cs |