Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 프로그래머스
- oracle ansi sql
- 개발자 면접 준비
- 이중우선순위큐 java
- DBASE&
- oracle ansi
- 면접 필수 질문
- 이중우선순위큐 자바
- 그리디
- 위상정렬
- 백트래킹
- 디베이스앤
- 이분탐색
- JPA
- DFS
- CJ DBASE&
- ansi sql 단점
- 프로그래머스 이중우선순위큐 java
- Gradle
- SQL
- IT 면접 준비
- 디베이스앤 인턴 후기
- 프로그래머스 이중우선순위큐
- Spring Boot
- 프로그래머스 이중우선순위큐 자바
- ansi sql 장점
- Java
- BFS
- 백준
- DP
Archives
- Today
- Total
쉬운 프로그래밍
[알고리즘] 백준 2503 숫자야구 - JAVA 본문
입력으로 주어진 숫자야구에 대한 스트라이크 / 볼 값을 모두 만족하는 경우의 수를 찾는 문제이다.
중복된 숫자 ex) 122 와 0이 들어갈 수 없는 제한이 있다.
그러므로 123 ~ 987에 대해서 중복되는 경우와 0인 경우를 걸러 모두 반복문을 돌려주면 된다.
알고리즘적인 어려움은 없고 구현력이 필요한 문제같다.
브루트포스 유형을 많이 풀어보면서 구현력을 늘려야 할 것 같다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.StringTokenizer;
class Main {
static int n;
static List<BaseBallData> inputData = new ArrayList<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
n = Integer.parseInt(s);
for (int i = 0; i < n; i++) {
s = br.readLine();
StringTokenizer st = new StringTokenizer(s, " ");
int data = Integer.parseInt(st.nextToken());
int strike = Integer.parseInt(st.nextToken());
int ball = Integer.parseInt(st.nextToken());
BaseBallData baseBallData = new BaseBallData(data, strike, ball);
inputData.add(baseBallData);
}
System.out.println(calBaseBall());
}
static int calBaseBall() {
int result = 0;
for (int i = 123; i <= 987; i++) {
if (!checkSameNum(i)) continue; // 중복숫자
int allTestPass = 0;
for (int j = 0; j < n; j++) {
int strikeCount = 0;
int ballCount = 0;
BaseBallData current = inputData.get(j);
String currentDataString = Integer.toString(current.data);
String myDataString = Integer.toString(i);
for (int k = 0; k < 3; k++) { // strike
if (currentDataString.charAt(k) == myDataString.charAt(k)) {
strikeCount++;
}
}
for (int h = 0; h < 3; h++) {
for (int u = 0; u < 3; u++) {
if (myDataString.charAt(h) == currentDataString.charAt(u)) { // ball
if (h != u)
ballCount++;
}
}
}
if (current.strike == strikeCount && current.ball == ballCount) {
allTestPass++;
}
}
if (allTestPass == n) {
result++;
}
}
return result;
}
static boolean checkSameNum(int num) {
String numString = Integer.toString(num);
if (numString.charAt(0) == numString.charAt(1)) {
return false;
}
if (numString.charAt(1) == numString.charAt(2)) {
return false;
}
if (numString.charAt(0) == numString.charAt(2)) {
return false;
}
if (numString.charAt(0) == '0' || numString.charAt(1) == '0' || numString.charAt(2) == '0')
return false;
return true;
}
static class BaseBallData {
int data;
int strike;
int ball;
public BaseBallData(int data, int strike, int ball) {
this.data = data;
this.strike = strike;
this.ball = ball;
}
}
}
'알고리즘 > Brute Force' 카테고리의 다른 글
[알고리즘] 프로그래머스 - 카펫 - JAVA (1) | 2021.08.18 |
---|---|
[알고리즘] 프로그래머스 - 모의고사 - JAVA (0) | 2021.08.14 |
[알고리즘] 백준 2422 한윤정이 이탈리아에 가서 아이스크림을 사먹는데 - JAVA (0) | 2021.04.15 |
[알고리즘] 백준 2798 블랙잭 - JAVA (0) | 2021.03.24 |
Comments