• 문제 링크
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;
}
}
'Problem Solving > Baekjoon' 카테고리의 다른 글
[백준] 6131 완전 제곱수 - Brute Force / Java (0) | 2024.04.24 |
---|---|
[백준] 1980 햄버거 사랑 - Brute Force / Java (0) | 2024.04.23 |
[백준] 11723 집합 - Implementation / Java (0) | 2024.04.21 |
[백준] 5622 다이얼 - Implementation / Java (0) | 2024.04.20 |
[백준] 2941 크로아티아 알파벳 - Implementation / Java (0) | 2024.04.19 |
댓글