Baekjoon Algorithm | Problem 2309: Seven Dwarfs
Source
https://www.acmicpc.net/problem/2309
Problem
Solve Baekjoon Online Judge problem 2309, Seven Dwarfs.
Input
Follow the input format and constraints given in the original problem statement.
Output
Print the answer required by the problem.
Sample Input 1
20 7 23 19 10 15 25 8 13
Sample Output 1
7 8 10 13 19 20 23
Algorithm Classification
- Brute Force
- Sorting
Solution
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
int sum = 0;
int[] arr = new int[9];
try (BufferedReader br = new BufferedReader(new InputStreamReader(System.in))) {
for(int i= 0; i < 9;i++){
arr[i] = Integer.parseInt(br.readLine());
sum += arr[i];
}
}
Arrays.sort(arr);
boolean exit = false;
for (int i = 0; i < arr.length - 1; i++) {
for (int j = i; j < arr.length; j++) {
if (sum - arr[i] - arr[j] == 100) {
for (int y = 0; y < arr.length; y++) {
if(arr[i] == arr[y] || arr[j] == arr[y]) {
continue;
}
System.out.println(arr[y]);
}
exit =true;
break;
}
}
if (exit) {
break;
}
}
}
}