본문 바로가기
알고리즘/SWEA

[Java] SWEA 5215번 햄버거 다이어트

by 컴공맨 2021. 1. 29.
 

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