티스토리 뷰
SWEA 햄버거 다이어트 (D 3)
package test;
import java.util.*;
import java.io.FileInputStream;
public class Swea5215 {
static int[] score;
static int[] calory;
static int n;
static int max;
public static void main(String args[]) throws Exception
{
//System.setIn(new FileInputStream("res/input.txt"));
/*
표준입력 System.in 으로부터 스캐너를 만들어 데이터를 읽어옵니다.
*/
Scanner sc = new Scanner(System.in);
int T;
T=sc.nextInt();
/*
여러 개의 테스트 케이스가 주어지므로, 각각을 처리합니다.
*/
for(int test_case = 1; test_case <= T; test_case++)
{
n = sc.nextInt();
int cal = sc.nextInt(); // 최대 칼로리
score = new int[n];
calory = new int[n];
max = 0;
for(int i = 0; i < n; i++) {
score[i] = sc.nextInt();
calory[i] = sc.nextInt();
}
run(0, 0, 0, cal);
System.out.println("#" + test_case + " " + max);
}
}
static void run(int level, int sum, int scores, int cal) {
if(sum > cal) {
return;
}
if(level == n) {
if(sum <= cal) {
max = Math.max(scores, max);
}
return;
}
run(level + 1, sum + calory[level], scores + score[level], cal);
run(level + 1, sum, scores, cal);
}
}
KnapSack (배낭) 문제
n개의 배낭과 물건이 있을 때 배낭의 최대 용량을 초과하지 않고 배당에 담을 수 있는 물건의 최대 가치 합을 구하는 문제
재귀 함수로 풀 수도 있다.
👉 나(level)을 포함하며 탐색하거나 포함하지 않고 탐색하는 방법
run(level + 1, sum + calory[level], scores + score[level], cal);
run(level + 1, sum, scores, cal);
'알고리즘' 카테고리의 다른 글
이진 탐색 - Upper Bound와 Lower Bound (0) | 2025.01.25 |
---|---|
[SWEA] 0/1 Knapsack - 3282 (0) | 2024.11.17 |
[14002] 가장 긴 증가하는 부분 수열 4 - Java (1) | 2024.11.15 |
0-1 BFS (1) | 2024.04.04 |
[4485] 녹색 옷 입은 애가 젤다지? - Java (0) | 2024.02.05 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- 백준
- ddl-auto
- @ConfigurationProperties
- springboot
- StreamAPI
- lowerBound
- Java
- null
- 메인메소드
- 일급컬렉션
- 이진탐색
- @Value
- upperBound
- 유효성 검사
- 자바
- Optional
- NPE
- id생성전략
- 오블완
- 티스토리챌린지
- 동등성
- Thymeleaf
- @NoArgsConstructor
- JPA
- Spring
- N+1문제
- 생성자
- checkedException
- uncheckedException
- @Spring
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |
글 보관함