๐Algorithm/๐ฅBaekjoon
[Baekjoon] 11809_YODA
๋ฟ์ผ._.
2025. 5. 1. 22:26
๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/11809)
< YODA >
๋ฌธ์ ํ์ด
๊ฐ ์๋ฆฟ์๋ฅผ ๋น๊ตํ์ฌ ์์ ๊ฐ์ ์ ๊ฑฐํ๋ค.
my solution (Java)
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class _11809_ { // YODA
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
String a = bf.readLine();
String b = bf.readLine();
char arr[] = new char[a.length()];
char arr2[] = new char[b.length()];
for (int i = 0; i < a.length(); i++) {
arr[i] = a.charAt(i);
}
for (int i = 0; i < b.length(); i++) {
arr2[i] = b.charAt(i);
}
int min = Math.min(a.length(), b.length());
for (int i = 0; i < min; i++) {
if (arr[a.length() - 1 - i] < arr2[b.length() - 1 - i]) {
arr[a.length() - 1 - i] = ' ';
} else if (arr[a.length() - 1 - i] > arr2[b.length() - 1 - i]) {
arr2[b.length() - 1 - i] = ' ';
}
}
String result = new String(arr).replace(" ", "");
if (result.equals("")) {
System.out.println("YODA");
} else {
System.out.println(Integer.parseInt(result));
}
result = new String(arr2).replace(" ", "");
if (result.equals("")) {
System.out.println("YODA");
} else {
System.out.println(Integer.parseInt(result));
}
}
}
๋ณ์)
a, b : ์ฃผ์ด์ง ๊ฐ
arr, arr2 : a, b๋ฅผ ๋ฐฐ์ด์ ์ ์ฅ
min : a, b ์ค ์งง์ ๊ธธ์ด
result : ์์ ํ ๊ฒฐ๊ณผ
์ฃผ์ด์ง ๊ฐ 2๊ฐ๋ฅผ ์ ๋ ฅ๋ฐ์ ๊ฐ ๋ฐฐ์ด์ ์ ์ฅํ๋ค. ๋ท์๋ฆฌ๋ถํฐ ๊ฐ์ ๋น๊ตํ๋ฉด์ ์์ ๊ฐ์ ์ ๊ฑฐํ๋ค. ๋จ์ ๊ฐ์ ํ์ธํ์ ๋ ์๋ฌด๊ฒ๋ ์๋ค๋ฉด "YODA"๋ฅผ ์ถ๋ ฅํ๊ณ , ๊ทธ๋ ์ง ์๋ค๋ฉด ๋จ์ ๊ฐ์ ์ถ๋ ฅํ๋ค.
