Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified kokeunho/.DS_Store
Binary file not shown.
1 change: 1 addition & 0 deletions kokeunho/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) |
---
108 changes: 108 additions & 0 deletions kokeunho/κ΅¬ν˜„/33-kokeunho.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
import java.util.*;

class Solution {
public String[] solution(String[] expressions) {
//μ΅œμ†Œ κ°€λŠ₯ 진법 μ°Ύκ³  X ν¬ν•¨λœ 식 λ”°λ‘œ λΆ„λ¦¬ν•˜κΈ°
List<String> 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<Integer> 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<String> 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();
}
}