숫자야구
풀이
우선, 어떻게 풀어야 할 지 감은 오지만, 막상 손으로 구현이 되지 않아 작성하다가 다른 이의 코드를 보게 되었다. 잠깐 힌트를 얻고 바로 풀이에 들어가서 솔브를 얻었다.
(1) 항상 세자리이고, 그 세자리는 모두 다른 값이다. 따라서 1 ~ 9 까지의 수를 for 문 3번 돌린다.
(2) 주어진 게임에 대한 값들과 내가 만들어 놓은 값(for문 3번 돌려 얻은 값) 을 비교한다.
(3) 비교하는 과정에서 주어진 게임에서 물어본 값의 결과내용 strike 와 ball 이 내가 만들어 놓은 값의 결과와 일치하는지 비교하는 과정을 거친다.
(4) 무난하게 비교가 다 되면 내가 만들어 놓은 값은 정답으로 생각해 볼 수 있다.
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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 | import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStreamWriter; import java.util.ArrayList; import java.util.StringTokenizer; public class Main { private static final File file = new File("src\\input.txt"); private static FileInputStream fis = null; static InputStreamReader isr; static BufferedReader br; static OutputStreamWriter osw; static BufferedWriter bw; static StringTokenizer st; static int N = 0; public static void main(String[] args) throws IOException { // fis = new FileInputStream(file); // isr = new InputStreamReader(fis); isr = new InputStreamReader(System.in); br = new BufferedReader(isr); osw = new OutputStreamWriter(System.out); bw = new BufferedWriter(osw); int N = Integer.parseInt(br.readLine()); ArrayList<int[]> game = new ArrayList<int[]>(); while(N > 0) { st = new StringTokenizer(br.readLine()); int[]value = new int[3]; value[0] = Integer.parseInt(st.nextToken()); value[1] = Integer.parseInt(st.nextToken()); value[2] = Integer.parseInt(st.nextToken()); game.add(value); N--; } int result = 0; for(int i = 1; i <= 9; i++) { for(int j = 1; j <= 9; j++) { for(int k = 1; k <= 9; k++) { if(i == j || j == k || i == k) { continue; } String askNum = i + "" + j + "" + k; if(isPossible(game, askNum)){ result += 1; } } } } bw.write(result + "\n"); bw.flush(); bw.close(); br.close(); } private static boolean isPossible(ArrayList<int[]> game, String paramAskNum) { char[] askNum = paramAskNum.toCharArray(); int trueCount = 0; for(int value[] : game) { String num = String.valueOf(value[0]); int strike = value[1]; int ball = value[2]; char[]numArray = num.toCharArray(); int localStrike = 0; int localBall = 0; for(int i = 0; i < 3; i++) { for(int j = 0; j < 3; j++) { if(numArray[i] == askNum[j]) { if(i == j) { localStrike += 1; } else { localBall += 1; } } } } if(strike == localStrike && ball == localBall) { trueCount += 1; } } if(trueCount == game.size()) { return true; } return false; } } | cs |
'Problem Solving & Algorithm > BOJ ' 카테고리의 다른 글
20181225 1149번 : 수리공 항승 (0) | 2018.12.25 |
---|---|
20181225 1182번 : 부분집합의 합 (0) | 2018.12.25 |
20181223 3085번 : 사탕 게임 (0) | 2018.12.23 |
20180826 8월 BOJ 풀이 : 트리의 순회 (0) | 2018.08.26 |
20180825 8월 BOJ 풀이 : 트리의 높이와 너비 (0) | 2018.08.25 |