2503번: 숫자 야구
첫째 줄에는 민혁이가 영수에게 몇 번이나 질문을 했는지를 나타내는 1 이상 100 이하의 자연수 N이 주어진다. 이어지는 N개의 줄에는 각 줄마다 민혁이가 질문한 세 자리 수와 영수가 답한 스트
www.acmicpc.net
문제 요약
서로 다른 숫자 세개로 구성된 세자리 수
스트라이크 : 동일한 숫자, 동일한 위치
볼 : 동일한 숫자, 다른 자리
숫자를 불렀을 때 스트라이크, 볼 답한 걸 추측해서 가능성 있는 답의 총 개수 출력
시간 제한
1
입력
1 이상 100 이하의 자연수 N
질문한 숫자 , 스트라이크 개수를 나타내는 정수, 볼의 개수를 나타내는 정수가 N개의 줄에 주어짐
출력
영수가 생각하고 있을 가능성이 있는 답의 총 개수를 출력
접근법
방식1. 포문을 이용해 1~999까지 탐색하면서, N개의 물음과 답한 내용에 해당하는 숫자인지 찾기
코드
import java.io.*;
import java.util.StringTokenizer;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
int N = Integer.parseInt(br.readLine());
int[][] cases = new int[N][3];
for(int i=0; i<N; i++){
st = new StringTokenizer(br.readLine());
cases[i][0] = Integer.parseInt(st.nextToken());
cases[i][1] = Integer.parseInt(st.nextToken());
cases[i][2] = Integer.parseInt(st.nextToken());
}
int cnt = 0;
int first, second, third, num, tmpFirst, tmpSecond, tmpThird, strike, ball;
for(int i=123; i<999; i++){
first = i/100;
second = i/10%10;
third = i%10;
if( second == 0 || third == 0 ) continue;
if( first == second || second == third || first == third) continue;
boolean check = true;
for(int j=0; j<N; j++){
num = cases[j][0];
tmpFirst = num/100;
tmpSecond = num/10%10;
tmpThird = num%10;
strike = 0;
ball =0;
if(first == tmpFirst) strike++;
if(second == tmpSecond) strike++;
if(third == tmpThird) strike++;
if(first!=tmpFirst && (first == tmpSecond || first == tmpThird)) ball++;
if(second!=tmpSecond && (second == tmpFirst || second == tmpThird)) ball++;
if(third!=tmpThird && (third == tmpFirst || third == tmpSecond)) ball++;
if(strike != cases[j][1] || ball != cases[j][2]){
check = false;
break;
}
}
if(check) {
cnt++;
}
}
System.out.println(cnt);
}
}
'📙 Algorithm Solving > Java' 카테고리의 다른 글
📙[Algo] 24.01.07 알고리즘 (0) | 2024.01.07 |
---|---|
📙[Algo] 24.01.06 알고리즘 (0) | 2024.01.06 |
📙[Algo] 24.01.04 알고리즘 (0) | 2024.01.04 |
📙[Algo] 24.01.03 알고리즘 (0) | 2024.01.03 |
📙 [Algo] 24.01.02 알고리즘 (0) | 2024.01.02 |