티스토리 뷰

PS/boj

boj)2529 - 부등호

kingsubin 2020. 12. 8. 13:28
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
53
import java.io.*;
import java.util.*;
 
public class boj_2529 {
    static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer st;
    static List<String> ans = new ArrayList<>();
    static int n;
    static char[] input;
    static char[] a;
    static boolean[] v = new boolean[10];
 
    public static void main(String[] args) throws IOException {
        n = Integer.parseInt(br.readLine());
        input = new char[n];
        a = new char[n+1];
        st = new StringTokenizer(br.readLine());
        for (int i = 0; i < n; i++) {
            input[i] = st.nextToken().charAt(0);
        }
 
        go(0"");
        Collections.sort(ans);
        System.out.println(ans.get(ans.size()-1));
        System.out.println(ans.get(0));
    }
 
    static void go(int index, String num) {
        if (index == n+1) {
            ans.add(num);
            return;
        }
        for (int i = 0; i <= 9; i++) {
            if (v[i]) continue;
            if (index == 0 || good(num.charAt(index-1), (char)(i+'0'), input[index-1])) {
                v[i] = true;
                go(index + 1, num + i);
                v[i] = false;
            }
        }
    }
 
    static boolean good(char x, char y, char op) {
        if (op == '<') {
            if (x > y) return false;
        }
        if (op == '>') {
            if (x < y) return false;
        }
        return true;
    }
}
 
cs

 

- 브루트포스, 백트래킹

 

- 나는 MAX, MIN 따로 구하고 값도 String 말고 Long으로 해서 풀었는데 너무 코드가 길어서 다른 코드를 보았더니 훨씬 깔끔하고 좋았다.

- 좀 더 좋게 풀 수 있게 해보자.

 

 

 

 


www.acmicpc.net/problem/2529

'PS > boj' 카테고리의 다른 글

boj)11723 - 집합  (0) 2020.12.09
boj)14391 - 종이 조각  (0) 2020.12.09
boj)15661 - 링크와 스타트  (0) 2020.12.06
boj)14889 - 스타트와 링크  (0) 2020.12.03
boj)14501 - 퇴사  (0) 2020.12.02