SW Expert Academy
SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!
swexpertacademy.com
풀이
재귀함수를 이용해서 정해진 칼로리가 될 때 까지 모든 조합을 구해서 최대 값을 구하였습니다.
코드
import java.util.Scanner;
public class Solution {
public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
int tc = sc.nextInt();
for (int idx = 1; idx <= tc; ++idx) {
answer = 0;
n = sc.nextInt();
l = sc.nextInt();
for (int i = 0; i < n; ++i)
{
score[i] = sc.nextInt();
kcal[i] = sc.nextInt();
}
solve(0, 0, 0);
System.out.println("#" + idx + " " + answer);
}
sc.close();
}
static int[] score = new int[21];
static int[] kcal = new int[21];
static int answer = 0;
static int n, l;
static void solve(int idx, int sumScore, int sumKcal) {
if (idx == n) {
if (answer < sumScore && sumKcal <= l) {
answer = sumScore;
}
return;
}
solve(idx + 1, sumScore + score[idx], sumKcal + kcal[idx]);
solve(idx + 1, sumScore, sumKcal);
}
}
pyo7410/Algorithm
1일 1커밋을 목표로! Contribute to pyo7410/Algorithm development by creating an account on GitHub.
github.com
'알고리즘 > SWEA' 카테고리의 다른 글
[Java] SWEA 6019번 기차사이의 파리 (0) | 2021.02.02 |
---|---|
[Java] SWEA 3376번 파도반 수열 (0) | 2021.02.01 |
[Java] SWEA 2805번 농작물 수확하기 (0) | 2021.01.31 |
[Java] SWEA 6808번 규영이와 인영이의 카드게임 (0) | 2021.01.27 |
[Java] SWEA 8104번 조 만들기 (0) | 2021.01.26 |