티스토리 뷰
출저 : https://www.acmicpc.net/problem/14888
"브루트포스"
다해보자~
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 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.StringTokenizer; public class Main { static int N; static int num[]; static int op[] = new int[4]; static int max = Integer.MIN_VALUE; static int min = Integer.MAX_VALUE; public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); N = Integer.parseInt(br.readLine()); num = new int[N]; StringTokenizer st = new StringTokenizer(br.readLine()); for (int i = 0; i < N; i++) { num[i] = Integer.parseInt(st.nextToken()); } st = new StringTokenizer(br.readLine()); for (int i = 0; i < 4; i++) { op[i] = Integer.parseInt(st.nextToken()); } solve(1,num[0],op[0],op[1],op[2],op[3]); System.out.println(max); System.out.println(min); } static void solve(int idx, int sum, int op1, int op2, int op3, int op4 ) { if(idx == N) { max = Math.max(max, sum); min = Math.min(min, sum); return ; } if(op1 > 0) { solve(idx+1, sum +num[idx], op1-1,op2,op3,op4 ); } if(op2 > 0) { solve(idx+1, sum -num[idx], op1,op2-1,op3,op4 ); } if(op3 > 0) { solve(idx+1, sum *num[idx], op1,op2,op3-1,op4 ); } if(op4 > 0) { solve(idx+1, sum /num[idx], op1,op2,op3,op4-1 ); } } } | cs |
'Study > 알고리즘 문제풀이' 카테고리의 다른 글
2018 카카오 1차 코딩테스트 비밀지도 :: 돼지개발자 (0) | 2019.01.23 |
---|---|
백준 14499. 주사위 굴리기 :: 돼지개발자 (0) | 2019.01.21 |
백준 14890. 경사로 :: 돼지개발자 (0) | 2019.01.19 |
백준 15686. 치킨 배달 :: 돼지개발자 (0) | 2019.01.17 |
백준 14500. 테트로미노 :: 돼지개발자 (0) | 2019.01.17 |
댓글
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday