쿠릉쿠릉 쾅쾅
쿠릉쿠릉 쾅쾅
쿠릉쿠릉 쾅쾅
250x250
전체 방문자
오늘
어제
  • 분류 전체보기
    • HTML CSS
    • 잡담
    • 프로그래밍 꿀팁 사이트
    • 코딩 도서
    • [자바]
      • 디자인 패턴
      • 자바의 정석 - 3판
      • 자바
      • 자바 문법
    • git
    • [TDD]
    • 개발 서적 독후감
      • 클린 코더
      • 토비 스프링3
      • 객체지향의 사실과 오해
      • 모던 자바 인 액션
      • 엘레강트 오브젝트
    • CS
      • 운영체제
      • HTTP
    • [SQL]
      • SQL 기초
      • 혼자공부하는SQL
    • [ Spring ]
      • REST API
      • Spring Toy
      • Spring 에러
      • Spring
      • Spring 입문
      • Spring 핵심 원리
      • SpringMVC 1편
      • SpringMVC 2편
      • Spring Boot를 이용한 RESTful We..
      • Batch
    • [JPA]
      • JPA
      • JPA 에러
      • JPA 프로그래밍 - 기본편
      • 스프링 부트와 JPA 활용 1 - 웹 애플리케이..
      • 실전! 스프링 부트와 JPA 활용2 - API 개..
      • 실전! 스프링 데이터 JPA
      • 실전! Querydsl
    • 인텔리제이
    • [DB]
      • DB
      • H2
    • Gradle
    • 면접
    • [알고리즘]
      • 알고리즘
      • 자료구조
      • 자바 알고리즘 공부
    • [프로젝트]
    • 쿠릉식 객체지향 사고
    • 리눅스

블로그 메뉴

  • 홈
  • 태그
  • 방명록

공지사항

인기 글

태그

  • 스프링
  • http
  • 재귀
  • querydsl
  • REST API
  • java
  • Git
  • MVC
  • JPA
  • SQL
  • GitHub
  • springboot
  • 깃허브
  • 백준
  • 알고리즘
  • Spring
  • 자료구조
  • 함수형인터페이스
  • 자바
  • 스프링부트

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
쿠릉쿠릉 쾅쾅

쿠릉쿠릉 쾅쾅

[알고리즘]/알고리즘

[알고리즘] SW Expert Acadamy 5215. 햄버거 다이어트

2022. 2. 17. 18:14
728x90

 

 

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AWT-lPB6dHUDFAVT&categoryId=AWT-lPB6dHUDFAVT&categoryType=CODE&problemTitle=5215&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=&pageSize=10&pageIndex=1&&&&&&&&& 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

import java.util.Scanner;

public class Solution {
	
	static int N;
	static int L;
	static int max;
	
	static int[] arrN;
	static int[] arrL;
	
	static boolean[] check;
	
    public static void main(String[] args) {
    	
    	Scanner sc = new Scanner(System.in);
    	
    	int T = sc.nextInt();
    	
    	for(int t=1; t<=T; t++) {
    		N = sc.nextInt();
    		L = sc.nextInt();
    		
    		arrN = new int[N];
    		arrL = new int[N];
    		check = new boolean[N];
    		
    		for(int i=0; i<N; i++) {
    			arrN[i] = sc.nextInt();
    			arrL[i] = sc.nextInt();
    		}

    		max=0;
    		DFS(0);
    		System.out.println("#" + t + " " + max);
    		
    	}
   }
    
   static void DFS(int cnt) {
	   if(cnt==N) {
		   int sumL = 0;
		   int sumN = 0;
		   for(int i=0; i<check.length; i++) {
			   if(!check[i]) continue;
			   sumL += arrL[i];
			   sumN += arrN[i];
		   }
		   
		   if(sumL > L) return;
		   
		   max = Math.max(max, sumN);
		   
		   return;
		   
	   }
	   check[cnt] = true;
	   DFS(cnt+1);
	   check[cnt] = false;
	   DFS(cnt+1);
   }
   
}

 

 

 

📌 참고할만한 풀이

package algorithm_lab.day03.q1;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;

public class Hamburger {
   
   static int N, L,max_score;
   static int[] jumsu,cal;
   public static void main(String[] args) throws NumberFormatException, IOException {
      System.setIn(new FileInputStream("./src/algorithm_lab/day03/q1/sample_input.txt"));
      BufferedReader br= new BufferedReader(new InputStreamReader(System.in));
      
      int test_case=Integer.parseInt(br.readLine());
      
      for(int T=1;T<=test_case;T++) {
         String[] s= br.readLine().split(" ");
         
         N= Integer.parseInt(s[0]);
         L=Integer.parseInt(s[1]);
         jumsu= new int[N];
         cal=new int[N];
         max_score=0;
         
         for(int i=0;i<N;i++) {
            String[] s2=br.readLine().split(" ");
            jumsu[i]=Integer.parseInt(s2[0]);
            cal[i]=Integer.parseInt(s2[1]);
         }
         
         check(0,0,0);
         
         StringBuilder sb=new StringBuilder();
         sb.append("#").append(T).append(" ").append(max_score).append("\n");
         System.out.print(sb.toString());
      }
   }
   
   public static void check(int cnt, int total_score, int total_cal) {
      //기저 조건
      if(total_cal>L) return;
      if(cnt==N) {
         max_score=Math.max(total_score, max_score);
         return;
      }
      
      //해당 재료를 선택 할 때
      check(cnt+1,total_score+jumsu[cnt],total_cal+cal[cnt]);
      // 해당 재료를 선택하지 않을 때
      check(cnt+1,total_score,total_cal);
      
   }
}
728x90

'[알고리즘] > 알고리즘' 카테고리의 다른 글

[알고리즘] 백준 1759. 암호 만들기  (0) 2022.02.22
[알고리즘] 백준 1182. 부분수열의 합  (0) 2022.02.17
[알고리즘] 백준 1991. 트리순회  (0) 2022.02.13
[알고리즘] 백준 2309번 일곱 난쟁이  (0) 2022.02.13
[알고리즘] SW Expert Academy 3499. 퍼펙트 셔플  (0) 2022.02.13
    '[알고리즘]/알고리즘' 카테고리의 다른 글
    • [알고리즘] 백준 1759. 암호 만들기
    • [알고리즘] 백준 1182. 부분수열의 합
    • [알고리즘] 백준 1991. 트리순회
    • [알고리즘] 백준 2309번 일곱 난쟁이
    쿠릉쿠릉 쾅쾅
    쿠릉쿠릉 쾅쾅
    깃허브 주소 : https://github.com/kureung

    티스토리툴바