SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
import java.util.Scanner;
import java.io.FileInputStream;
import java.util.*;
class Solution
{
public static int resources, limitCalories, maxScore;
public static int[] score, calories;
public static void main(String args[]) throws Exception
{
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
for(int test_case = 1; test_case <= T; test_case++)
{
resources = sc.nextInt(); limitCalories = sc.nextInt();
score= new int[resources];
calories = new int[resources];
for(int i=0; i<resources; i++){
score[i] = sc.nextInt();
calories[i] = sc.nextInt();
}
maxScore = 0;
combination(0,0,0);
System.out.println("#" + test_case + " " + maxScore);
}
}
private static void combination(int scoreSum, int caloriesSum, int index){
if(caloriesSum>=limitCalories)
return;
if(index == resources){
maxScore = Math.max(maxScore, scoreSum);
return;
}
combination(scoreSum + score[index], caloriesSum + calories[index], index +1);
combination(scoreSum, caloriesSum, index + 1);
}
}
N-queen 문제와 유사하다.
'SWEA' 카테고리의 다른 글
[SWEA] Sum (0) | 2024.11.16 |
---|---|
[SWEA] Flatten (1) | 2024.11.16 |
[SWEA] [S/W 문제해결 기본] 1일차 - View (2) | 2024.11.15 |
[SWEA] 등차수열 만들기 (1) | 2024.11.05 |
[SWEA] 식료품 가게 (0) | 2024.10.29 |