백준 16938 캠프 준비 (Java,자바)

jonghyukLee·2022년 1월 6일
0

이번에 풀어본 문제는
백준 16938번 캠프 준비 입니다.

📕 문제 링크

❗️코드

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
    static int N,L,R,X;
    static int [] map;
    static int tmp_cnt,tmp_min,tmp_answer;
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st = new StringTokenizer(br.readLine());

        N = Integer.parseInt(st.nextToken());
        L = Integer.parseInt(st.nextToken());
        R = Integer.parseInt(st.nextToken());
        X = Integer.parseInt(st.nextToken());

        map = new int[N];
        st = new StringTokenizer(br.readLine());
        for(int i = 0; i < N; ++i)
        {
            map[i] = Integer.parseInt(st.nextToken());
        }
        Arrays.sort(map);

        int answer = 0;
        for(int i = 0; i < N-1; ++i)
        {
            tmp_cnt = N-i;
            tmp_min = map[i];
            tmp_answer = 0;
            comb(i+1,1,1,map[i],map[i]);
            answer += tmp_answer;
        }
        System.out.print(answer);
    }
    static void comb(int idx, int cnt,int add_cnt,int max,int tot)
    {
        if(cnt == tmp_cnt)
        {
            if(add_cnt > 1)
            {
                if((max-tmp_min >= X) && (tot >= L && tot <= R)) tmp_answer++;
            }
            return;
        }

        for(int i = idx; i < N; ++i)
        {
            comb(i+1,cnt+1,add_cnt,max,tot);
            comb(i+1,cnt+1,add_cnt+1,map[i],tot+map[i]);
        }

    }
}

📝 풀이

주어진 조건에 맞게 문제를 고를 수 있는 개수를 구하는 문제입니다.
조합을 사용하여 모든 경우의 수를 만들고, 최종적으로 모든 조건에 부합할 경우만 카운트에 반영해줍니다.

📜 후기

조합만 구현한다면 어렵지 않은 문제인 것 같습니다.

profile
머무르지 않기!

0개의 댓글