[백준] 2798번 블랙잭 (java, 완전탐색)
728x90
반응형
2798번: 블랙잭
첫째 줄에 카드의 개수 N(3 ≤ N ≤ 100)과 M(10 ≤ M ≤ 300,000)이 주어진다. 둘째 줄에는 카드에 쓰여 있는 수가 주어지며, 이 값은 100,000을 넘지 않는 양의 정수이다. 합이 M을 넘지 않는 카드 3장
www.acmicpc.net
import java.util.Scanner;
public class Main {
static int[] card;
static int n,m;
static int answer;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
n=sc.nextInt();
m=sc.nextInt();
card=new int[n];
for (int i = 0; i < n; i++) {
card[i]=sc.nextInt();
}
comb(0,0,0);
System.out.println(answer);
}
private static void comb(int start, int sum,int cnt) {
if(sum>m)return;
if(cnt==3) {
if(sum==m) {
System.out.println(m);
System.exit(0);
}else if(sum>answer) {
answer=sum;
}
return;
}
for (int i = start; i < n; i++) {
comb(i+1,sum+card[i],cnt+1);
}
}
}
728x90
반응형
'백준' 카테고리의 다른 글
[백준] 19532번 수학은 비대면 강의입니다. (java, 완전탐색) (0) | 2021.04.09 |
---|---|
[백준] 2231번 분해합 (java, 완전탐색) (0) | 2021.04.09 |
[백준] 1212번 8진수 2진수 (java, 구현) (0) | 2021.04.05 |
[백준] 1774번 우주신과의 교감 (java, mst, 크루스칼) (0) | 2021.04.05 |
[백준] 16953번 A -> B (JAVA, DFS) (0) | 2021.03.30 |
TAGS.