반응형

@notepad_jj2

츄르사려고 코딩하는 코집사입니다.


1. [SW expert Academy] SWEA 5215번 햄버거 다이어트 자바(Java)

 

2. 코드

import java.util.Arrays;
import java.util.Scanner;
 
public class Solution {
    static int N;
    static int L;
    static int[] score;
    static int[] cal;
     
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
        for(int tc = 1; tc <= T; tc++) {
            N = sc.nextInt();
            L = sc.nextInt();
            
            score = new int[N];
            cal = new int[N];
            
            for(int i = 0; i < N; i++) {
                score[i] = sc.nextInt();
                cal[i] = sc.nextInt();
            }
            
            sel = new boolean[N];
            ans = 0;
            
            powerset(0);
            
            System.out.println("#" + tc + " " + ans);
        }
    }
    static boolean[] sel;
    static int ans = 0;
    static void powerset(int idx) {
        if( idx == N ) {
            int sumCal = 0;
            int sumScore = 0;
            
            for(int i = 0; i < N; i++) {
                if( sel[i] ) {
                    sumCal += cal[i];
                    sumScore += score[i];
                }
            }
            
            if( sumCal < limit && sumScore > ans)
                ans = sumScore;
                
            return;
        }
        
        sel[idx] = true;
        powerset(idx + 1);
        sel[idx] = false;
        powerset(idx + 1);
    }
}
반응형
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기