[swexpert] 9229. 한빈이와 Spot Mart (D3, java)
728x90
반응형
조합 문제이다. nC2를 구하면 된다.
import java.util.Scanner;
public class Solution {
static int t;
static int n,m;
static int[] snack;
static int answer;
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
t=sc.nextInt();
for (int tc = 1; tc <=t; tc++) {
n=Integer.parseInt(sc.next());
m=Integer.parseInt(sc.next());
answer=Integer.MIN_VALUE;
snack=new int[n];
for (int i = 0; i < n; i++) {
snack[i]=Integer.parseInt(sc.next());
}
check(0,0,0);
System.out.printf("#%d %d\n",tc,answer<0?-1:answer);
}
}
private static void check(int cnt,int weight,int start) {
if(cnt==2) {
if(weight>answer)answer=weight;
return;
}
for (int i = start; i < n; i++) {
if(weight+snack[i]<=m) {
check(cnt+1,weight+snack[i],i+1);
}
}
}
}
728x90
반응형
'swexpert' 카테고리의 다른 글
[swexpert] 1233. 사칙연산 유효성 검사 (JAVA) (0) | 2021.02.09 |
---|---|
[swexpert] 1940. 가랏! RC카! (D2, JAVA) (0) | 2021.02.08 |
[swexpert] 1228. 암호문1 (java, D3) (0) | 2021.02.08 |
[swexpert] 5215. 햄버거 다이어트 (java, D3) (0) | 2021.02.08 |
[swexpert] 3307. 최장 증가 부분 수열 (java, D3) (0) | 2021.02.05 |
TAGS.