-
[백준 2798] 블랙잭코딩테스트 문제풀이 2021. 10. 6. 22:21
문제
풀이
import java.util.Scanner; public class Main { static int max = 0; public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int m = scanner.nextInt(); int[] cards = new int[n]; for (int i = 0; i < n; i ++) { cards[i] = scanner.nextInt(); } scanner.close(); boolean[] used = new boolean[n]; permutation(cards, new int[3], 0, used, m); System.out.println(max); } public static void permutation(int[] cards, int[] selectedCards, int selectedCount, boolean[] used, int m) { if (selectedCount == 3) { int sum = 0; for (int card : selectedCards) { sum += card; } if (m >= sum && sum > max) { max = sum; } return; } for (int i = 0; i < cards.length; i++) { if (!used[i]) { used[i] = true; selectedCards[selectedCount] = cards[i]; permutation(cards, selectedCards, selectedCount + 1, used, m); used[i] = false; } } } }
- dfs를 이용한 완전탐색, 모든 3개 뽑는 경우의 수를 구했다.
'코딩테스트 문제풀이' 카테고리의 다른 글
[백준 1920] 수찾기 - 이분탐색 (0) 2021.10.07 [프로그래머스] 야근지수 (0) 2021.10.06 [프로그래머스] 베스트앨범 - 해시 (0) 2021.10.02 [백준 11279] 최대힙 - 우선순위 큐 (0) 2021.10.02 [프로그래머스] 소수 찾기 - 완전탐색 (0) 2021.10.02