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 | 29 | 30 | 31 |
Tags
- 연산자
- 생활코딩
- 딥러닝
- JavaScript
- 머신러닝
- Database
- pandas
- tensorflow
- 카카오클라우드스쿨2기
- 생활코딩 머신러닝야학
- 파이썬
- 머신러닝(딥러닝)
- LeNet
- 야학
- flatten
- CNN
- Java
- 이것이 자바다
- Python
- 데이터베이서
- 데이터베이스
- 생활코딩 데이터베이스
- 판다스
- 데이터베이스 개론
- reshape
- 개발자
- 머신러닝야학
- MySQL
Archives
- Today
- Total
IT's 우
[java]백준 14888번: 연산자 끼워넣기 본문
728x90
https://www.acmicpc.net/problem/14888
14888번: 연산자 끼워넣기
첫째 줄에 수의 개수 N(2 ≤ N ≤ 11)가 주어진다. 둘째 줄에는 A1, A2, ..., AN이 주어진다. (1 ≤ Ai ≤ 100) 셋째 줄에는 합이 N-1인 4개의 정수가 주어지는데, 차례대로 덧셈(+)의 개수, 뺄셈(-)의 개수,
www.acmicpc.net
코드
import java.util.*;
import java.io.*;
public class j14888 {
static int N;
static int[] A;
static int[] Sign;
static int max;
static int min;
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
N = Integer.parseInt(br.readLine());
StringTokenizer st = new StringTokenizer(br.readLine());
A = new int[N];
for (int i = 0; i < N; i++) {
A[i] = Integer.parseInt(st.nextToken());
}
Sign = new int[4];
st = new StringTokenizer(br.readLine());
for (int i = 0; i < 4; i++) {
Sign[i] = Integer.parseInt(st.nextToken());
}
max = Integer.MIN_VALUE;
min = Integer.MAX_VALUE;
Sum(1, A[0]);
System.out.println(max);
System.out.println(min);
}
private static void Sum(int count, int total) {
if (count == N) {
max = Math.max(max, total);
min = Math.min(min, total);
} else {
// +해주기
if (Sign[0] > 0) {
Sign[0]--;
Sum(count + 1, total + A[count]);
Sign[0]++;
}
// -해주기
if (Sign[1] > 0) {
Sign[1]--;
Sum(count + 1, total - A[count]);
Sign[1]++;
}
// 나누기 해주기
if (Sign[2] > 0) {
Sign[2]--;
Sum(count + 1, total * A[count]);
Sign[2]++;
}
// x해주기
if (Sign[3] > 0) {
Sign[3]--;
Sum(count + 1, total / A[count]);
Sign[3]++;
}
}
}
}
풀이
DFS 사용
728x90
반응형
'알고리즘 > 백준' 카테고리의 다른 글
[java]백준 11047번: 동전 0, 그리디 알고리즘 (1) | 2022.09.26 |
---|---|
[java]백준 1715번: 카드 정렬하기, 그리디 알고리즘 (0) | 2022.09.26 |
[java]백준 14889번- 스타트와 링크 (1) | 2022.09.21 |
[java]백준 25391번- 특별상 (0) | 2022.09.21 |
[java]백준 23842- 성냥개비 (1) | 2022.09.21 |