[swexpert] 5215. 햄버거 다이어트 (java, D3)
728x90
반응형
부분 집합으로 풀어주었다.
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.StringTokenizer;
public class Solution {
static int t,n,l;
static int[] scores;
static int[] calories;
static int answer;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
StringTokenizer st;
t=Integer.parseInt(br.readLine());
for (int tc = 1; tc <=t; tc++) {
answer=0;
st=new StringTokenizer(br.readLine());
n=Integer.parseInt(st.nextToken());
l=Integer.parseInt(st.nextToken());
scores=new int[n];
calories=new int[n];
for (int i = 0; i < n; i++) {
st=new StringTokenizer(br.readLine());
scores[i]=Integer.parseInt(st.nextToken());
calories[i]=Integer.parseInt(st.nextToken());
}
comb(0,0,0);
System.out.printf("#%d %d\n",tc,answer);
}
}
private static void comb(int cnt,int sum,int score) {
if(cnt==n) {
if(score>answer)answer=score;
return;
}
if(sum+calories[cnt]<=l) {
comb(cnt+1,sum+calories[cnt],score+scores[cnt]);
}
comb(cnt+1,sum,score);
}
}
728x90
반응형
'swexpert' 카테고리의 다른 글
[swexpert] 9229. 한빈이와 Spot Mart (D3, java) (0) | 2021.02.08 |
---|---|
[swexpert] 1228. 암호문1 (java, D3) (0) | 2021.02.08 |
[swexpert] 3307. 최장 증가 부분 수열 (java, D3) (0) | 2021.02.05 |
[swexpert] 1223. 계산기2 (java, D4) (0) | 2021.02.05 |
[swexpert] 3499. 퍼펙트 셔플 (JAVA, D3) (0) | 2021.02.05 |
TAGS.