백준2798: 블랙잭
문제
https://www.acmicpc.net/problem/2798
문제 풀이
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int N = sc.nextInt();
int M = sc.nextInt();
int[] deck = new int[N];
for (int i = 0; i < deck.length; i++) {
deck[i] = sc.nextInt();
}
int result = search(deck, M);
System.out.println(result);
}
public static int search(int[] deck, int M) {
int result = 0;
for (int i = 0; i < deck.length - 2; i++) {
for (int j = i + 1; j < deck.length - 1; j++) {
for (int k = j + 1; k < deck.length; k++) {
int tmp = deck[i] + deck[j] + deck[k];
if (M == tmp || (result < tmp && tmp < M)) {
result = tmp;
}
}
}
}
return result;
}
}
댓글남기기