문제(출처: 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"를 출력하고, 그렇지 않다면 남은 값을 출력한다.
'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글
[Baekjoon] 11999_Milk Pails (Bronze) (1) | 2025.05.07 |
---|---|
[Baekjoon] 5976_A spiral walk (1) | 2025.05.02 |
[Baekjoon] 28464_Potato (1) | 2025.04.30 |
[Baekjoon] 4335_숫자 맞추기 (0) | 2025.04.29 |
[Baekjoon] 11544_D as in Daedalus (1) | 2025.04.28 |