diff --git a/kokeunho/.DS_Store b/kokeunho/.DS_Store index 1256d34..57045c0 100644 Binary files a/kokeunho/.DS_Store and b/kokeunho/.DS_Store differ diff --git a/kokeunho/README.md b/kokeunho/README.md index c4b912d..d100262 100644 --- a/kokeunho/README.md +++ b/kokeunho/README.md @@ -32,4 +32,5 @@ | 29차시 | 2025.07.21 | 그래프 탐색 | [불!](https://www.acmicpc.net/problem/4179) |[#118](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/118) | | 30차시 | 2025.07.26 | 그래프 탐색 | [중량제한](https://www.acmicpc.net/problem/1939) |[#120](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/120) | | 31차시 | 2025.08.03 | 그래프 탐색 | [면접보는 승범이네](https://www.acmicpc.net/problem/17835) | [#126](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/126) | +| 33차시 | 2025.08.18 | 구현 | [수식 복원하기](https://school.programmers.co.kr/learn/courses/30/lessons/340210) | [#132](https://github.com/AlgoLeadMe/AlgoLeadMe-12/pull/132) | --- diff --git "a/kokeunho/\352\265\254\355\230\204/33-kokeunho.java" "b/kokeunho/\352\265\254\355\230\204/33-kokeunho.java" new file mode 100644 index 0000000..e59649a --- /dev/null +++ "b/kokeunho/\352\265\254\355\230\204/33-kokeunho.java" @@ -0,0 +1,108 @@ +import java.util.*; + +class Solution { + public String[] solution(String[] expressions) { + //최소 가능 진법 찾고 X 포함된 식 따로 분리하기 + List exprIncludeX = new ArrayList<>(); + int minBase = 2; + for (String expr : expressions) { + String[] parts = expr.split(" "); + minBase = Math.max(minBase, calculateMinBase(parts[0], parts[2], parts[4])); + if (parts[4].equals("X")) exprIncludeX.add(expr); + } + //가능한 진법들 중 식에 대입하여 검증되는 진법만 추리기 + List candBases = new ArrayList<>(); + for (int base = minBase; base <= 9; base++) { + boolean allValid = true; + for (String expr : expressions) { + String[] parts = expr.split(" "); + if (parts[4].equals("X")) continue; + if (!isValidExpression(parts[0], parts[2], parts[4], parts[1], base)) { + allValid = false; + break; + } + } + if (allValid) { + candBases.add(base); + } + } + // X 값 구하기 + String[] results = new String[exprIncludeX.size()]; + for (int i = 0; i < exprIncludeX.size(); i++) { + + String[] parts = exprIncludeX.get(i).split(" "); + + Set possibleResults = new HashSet<>(); + for (int base : candBases) { + int numA = toBase(parts[0], base); + int numB = toBase(parts[2], base); + int resultPerBase = switch(parts[1]) { + case "+" -> numA + numB; + case "-" -> numA - numB; + default -> throw new IllegalArgumentException(parts[1]); + }; + + possibleResults.add(fromBase(resultPerBase, base)); + } + if (possibleResults.size() == 1) { + results[i] = parts[0] + " " + parts[1] + " " + parts[2] + " = " + possibleResults.iterator().next(); + } else { + results[i] = parts[0] + " " + parts[1] + " " + parts[2] + " = ?"; + } + } + return results; + } + //가능한 진법 중 최소 진법을 찾기 + public int calculateMinBase(String... nums) { + int response = 2; + for (String num : nums) { + for (char c : num.toCharArray()) { + if (c != 'X') { + int digit = c - '0'; + response = Math.max(response, digit+1); + } + } + } + return response; + } + //수식에 해당 진법이 성립하는지 확인 + public boolean isValidExpression(String a, String b, String c, String op, int base) { + int numA = toBase(a, base); + int numB = toBase(b, base); + int numC = toBase(c, base); + + return switch(op) { + case "+" -> numA + numB == numC; + case "-" -> numA - numB == numC; + default -> false; + }; + } + //base 진법의 수를 10진법으로 수정 + public int toBase(String num, int base) { + int result = 0; + int power = 1; + for (int i = num.length() - 1; i >= 0; i--) { + int digit = num.charAt(i) - '0'; + result += digit * power; + power *= base; + } + return result; + } + //10진법의 수를 base진법으로 수정 + public String fromBase(int num, int base) { + if (num == 0) return "0"; + + //num이 음수인 경우 + boolean negative = num < 0; + if (negative) num = -num; + + StringBuilder result = new StringBuilder(); + while (num > 0) { + result.append(num % base); + num /= base; + } + if (negative) result.append("-"); + + return result.reverse().toString(); + } +} \ No newline at end of file