Skip to content

Commit e3a2d1b

Browse files
committed
issue #38 백준 2798 블랙잭
1 parent e2f8412 commit e3a2d1b

1 file changed

Lines changed: 56 additions & 0 deletions

File tree

‎src/backjoon/_2798.java‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package backjoon;
2+
// https://www.acmicpc.net/problem/2798
3+
// 블랙잭
4+
import java.io.BufferedReader;
5+
import java.io.IOException;
6+
import java.io.InputStreamReader;
7+
import java.util.StringTokenizer;
8+
9+
public class _2798 {
10+
public static void main(String[] args) throws IOException {
11+
// memory 11592 runtime 96
12+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
13+
StringTokenizer st = new StringTokenizer(br.readLine(), " ");
14+
int N = Integer.parseInt(st.nextToken());
15+
int M = Integer.parseInt(st.nextToken());
16+
17+
st = new StringTokenizer(br.readLine(), " ");
18+
int[] arr = new int[N];
19+
for(int i=0; i<arr.length; i++){
20+
arr[i] = Integer.parseInt(st.nextToken());
21+
}
22+
23+
int sum = findSum(arr, N, M);
24+
System.out.println(sum);
25+
}
26+
27+
static int findSum(int[] arr, int N, int M){
28+
int sum = 0;
29+
for(int i=0; i<N-2; i++){
30+
for(int j=i+1; j<N-1; j++){
31+
for(int k=j+1; k<N; k++){
32+
int tempSum = arr[i]+arr[j]+arr[k];
33+
if(tempSum == M){
34+
return tempSum;
35+
}
36+
// 세 카드의 합 sum이 이전 합 tempSum보다 크면서 M 보다 작을 경우
37+
if(sum < tempSum && tempSum < M) {
38+
sum = tempSum;
39+
}
40+
}
41+
}
42+
}
43+
return sum;
44+
}
45+
}
46+
/*
47+
INPUT
48+
5 21
49+
5 6 7 8 9
50+
51+
5 9
52+
1 2 4 3 5
53+
54+
OUTPUT
55+
21
56+
*/

0 commit comments

Comments
 (0)