728x90
반응형
2961번: 도영이가 만든 맛있는 음식
첫째 줄에 재료의 개수 N(1 ≤ N ≤ 10)이 주어진다. 다음 N개 줄에는 그 재료의 신맛과 쓴맛이 공백으로 구분되어 주어진다. 모든 재료를 사용해서 요리를 만들었을 때, 그 요리의 신맛과 쓴맛은
www.acmicpc.net
풀이
간단한 조합 문제
public class BOJ_2961 {
static int N;
static int M;
static int[][] material;
static int total;
public static void main(String[] args) throws NumberFormatException, IOException {
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(in.readLine());
material = new int[N][2];
for(int i=0;i<N;i++) {
StringTokenizer st = new StringTokenizer(in.readLine()," ");
material[i][0] = Integer.parseInt(st.nextToken());
material[i][1] = Integer.parseInt(st.nextToken());
}
total = Integer.MAX_VALUE;
for(int i=1;i<=N;i++) {
M = i;
comb(0,new int[M],0);
}
System.out.println(total);
}
static void comb(int toSelect, int[] selected, int startIdx) {
if(toSelect == M) {
int sour = 1, bitter = 0;
for(int i=0;i<M;i++) {
int idx = selected[i];
sour *= material[idx][0];
bitter += material[idx][1];
}
int res = Math.abs(sour-bitter);
total = Math.min(total, res);
return;
}
for(int i=startIdx;i<N;i++) {
selected[toSelect] = i;
comb(toSelect+1, selected, i+1);
}
}
}
728x90
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[문제] 백준 14503번 로봇 청소기 (0) | 2021.02.15 |
---|---|
[문제] 백준 1967번 트리의 지름 (0) | 2021.02.15 |
[문제] 백준 14225번 부분수열의 합 (0) | 2021.02.15 |
[문제] 백준 1182번 부분수열의 합 (0) | 2021.02.15 |
[문제] 백준 2609번 최대공약수와 최소공배수 (0) | 2021.02.15 |