Problem Solving/Baekjoon
[백준] 9873 Cow Baseball - Brute Force / Java
graycode
2024. 4. 22. 15:59
• 문제 링크
9873번: Cow Baseball
Input Details There are 5 cows, at positions 3, 1, 10, 7, and 4. Output Details The four possible triples are the cows as positions 1-3-7, 1-4-7, 4-7-10, and 1-4-10.
www.acmicpc.net
• 풀이 코드
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.util.Arrays;
public class Main {
public static void main(String[] args) throws IOException {
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int n = read();
int[] arr = new int[n];
for (int i = 0; i < n; i++) arr[i] = read();
Arrays.sort(arr);
int cnt = 0;
for (int i = 0; i < n - 2; i++)
for (int j = i + 1; j < n - 1; j++)
for (int k = j + 1; k < n; k++)
if (arr[j] - arr[i] <= arr[k] - arr[j] && (arr[j] - arr[i]) * 2 >= arr[k] - arr[j]) cnt++;
bw.write(String.valueOf(cnt));
bw.flush();
}
private static int read() throws IOException {
int c, n = System.in.read() & 15;
while ((c = System.in.read()) > 32) n = (n << 3) + (n << 1) + (c & 15);
return n;
}
}