문제 제목 : 블랙잭

난이도 : 하

문제 유형 : 배열, 완전 탐색

추천 풀이 시간 : 20분 (못하면 2배 40분)

https://www.acmicpc.net/problem/2798

 

2798번: 블랙잭

첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장

www.acmicpc.net

 

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[] arr = new int[n];
        for (int i = 0; i < arr.length; i++) {
            arr[i] = sc.nextInt();
        }
        
        int result = 0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                for (int k = j + 1; k < arr.length; k++) {
                    int sum = arr[i] + arr[j] + arr[k];
                    if (sum <= m) {
                        result = Math.max(result, sum);
                    }
                }
            }
        }
        
        System.out.println(result);
    }
}

'Algorithm > BAEKJOON' 카테고리의 다른 글

[알고리즘-DP] 파도반 수열  (0) 2023.01.20
[알고리즘-DP] 2×n 타일링  (0) 2023.01.20
[자료구조-큐] 프린터 큐  (0) 2023.01.20
[자료구조-스택] 스택 수열  (2) 2023.01.20
[자료구조-배열] 음계  (0) 2023.01.19

+ Recent posts