250x250
Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 정올 1620
- 백준13458
- 재귀함수
- N과M
- Bfs와DFS
- 전화번호속의암호
- 알고리즘개념
- 파티션 크기 조정
- 중복순열
- 완전탐색
- 알고리즘
- D드라이브생성
- 코테준비
- 코테
- 에라토스테네스의채
- 삼성역테
- 주사위굴리기2
- 자바
- 백준
- 백준2251
- java
- 23288
- 중복조합
- 백준15652
- 자바 코테
- 정보처리기사
- BFS
- 완탐
- 볼륨 만들기
- 순열
Archives
- Today
- Total
뚱땅뚱땅
[문제] 백준 2961번 도영이가 만든 맛있는 음식 본문
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 |
Comments