문제 링크 : https://www.acmicpc.net/problem/1541
문제 등급 : Silver II
 

1541번: 잃어버린 괄호

첫째 줄에 식이 주어진다. 식은 ‘0’~‘9’, ‘+’, 그리고 ‘-’만으로 이루어져 있고, 가장 처음과 마지막 문자는 숫자이다. 그리고 연속해서 두 개 이상의 연산자가 나타나지 않고, 5자리보다

www.acmicpc.net

문제 해결 로직

  1. 문자열을 '-' 기준으로 자른다.
  2. 자른 배열의 요소를 '+' 기준으로 자른다.
  3. +배열을 하나씩 더하고 - 를 한다.

이 문제는 +를 먼저 하고 -을 나중에 하여 큰 수를 빼주면 가장 작은 수를 구할 수 있는 문제이다.

 

아래와 같이 소스를 공유합니다.

public class 잃어버린 괄호 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] str = br.readLine().split("-");

        long res = Integer.MAX_VALUE;

        for(int i = 0; i < str.length; i++) {
            String[] s = str[i].split("\\+");
            
            long temp = 0;
            
            for(int j = 0; j < s.length; j++) {
                temp += Long.parseLong(s[j]);
            }
            
            if(res == Integer.MAX_VALUE) {
                res = temp;
            }else {
                res -= temp;
            }
        }

        System.out.println(res);
    }
}
문제 링크 : https://www.acmicpc.net/problem/1931
문제 등급 : Silver II
 

1931번: 회의실 배정

(1,4), (5,7), (8,11), (12,14) 를 이용할 수 있다.

www.acmicpc.net

문제 해결 로직

  1. 회의가 끝나는 시간을 기준으로 내림 차순 정렬하고 끝나는 시간이 동일하다면 시작 시간을 오름차순으로 정렬한다.
  2. 정렬된 끝나는 시간으로 회의의 수만큼 반복하면서 정렬된 첫 회의 끝나는 시간보다 같거나 큰 시간의 회의 시작시간을 찾아 회의를 진행한다.
  3. 회의의 끝나는 시간보다 먼저 시작하는 회의는 다른 회의실을 사용해야 하므로 제외한다.
  4. 회의를 진행 할때 마다 회의의 진행 수를 Count 한다.

 

아래와 같이 소스를 공유합니다

public class 회의실배정 {
    public static class Node {
        int s, e;
        Node(int s, int e) {
            this.s = s;
            this.e = e;
        }
    }
    public static List<Node> times = new ArrayList<>();
    public static Comparator<Node> compare = new Comparator<Node>() {
        @Override
        public int compare(Node o1, Node o2) {
            return Integer.compare(o1.e, o2.e) == 0 ? Integer.compare(o1.s, o2.s) : Integer.compare(o1.e, o2.e);
        }
    };
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(br.readLine());

        for (int i = 0; i < n; i++) {
            StringTokenizer st = new StringTokenizer(br.readLine(), " ");
            int s = Integer.parseInt(st.nextToken());
            int e = Integer.parseInt(st.nextToken());

            times.add(new Node(s, e));
        }

        times.sort(compare);

        Node node = times.get(0);

        int cnt = 1;
        for (int i = 1; i < times.size(); i++) {
            Node nextNode = times.get(i);
            if(node.e <= nextNode.s) {
                node = nextNode;
                cnt++;
            }
        }

        System.out.println(cnt);
    }
}

.

문제 링크 : https://www.acmicpc.net/problem/11047
문제 등급 : Silver II
 

11047번: 동전 0

첫째 줄에 N과 K가 주어진다. (1 ≤ N ≤ 10, 1 ≤ K ≤ 100,000,000) 둘째 줄부터 N개의 줄에 동전의 가치 Ai가 오름차순으로 주어진다. (1 ≤ Ai ≤ 1,000,000, A1 = 1, i ≥ 2인 경우에 Ai는 Ai-1의 배수)

www.acmicpc.net

문제 해결 로직

  1. 동전의 종류를 내림차순으로 정렬한다.
  2. 동전을 내림차순으로 하나씩 총금액보다 작은지 비교한다.
  3. 총금액보다 적은 금액으로 몇 번 나눌 수 있는지 계산한다.
  4. 나눈 개수를 Count하고 개수 * 금액을 총금액에서 뺀다.
  5. 2~4번을 반복하여 필요한 동전의 수를 구한다.

 

아래와 같이 소스를 공유합니다.

public class 동전0 {
    public static List<Integer> coins = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));

        String[] commends = br.readLine().split(" ");

        int n = Integer.parseInt(commends[0]);
        int tot = Integer.parseInt(commends[1]);

        for(int i = 0; i < n; i++) {
            int coin = Integer.parseInt(br.readLine());
            coins.add(coin);
        }

        coins.sort(Collections.reverseOrder());

        int cnt = 0;

        for(int i = 0; i < coins.size(); i++) {
            int unit = coins.get(i);

            if(unit <= tot) {
                int div = tot/unit;
                cnt += div;
                tot = tot - (div * unit);
            }
        }

        System.out.println(cnt);
    }
}
문제 링크 : https://www.acmicpc.net/problem/11399
문제 등급 : Silver II
 

11399번: ATM

첫째 줄에 사람의 수 N(1 ≤ N ≤ 1,000)이 주어진다. 둘째 줄에는 각 사람이 돈을 인출하는데 걸리는 시간 Pi가 주어진다. (1 ≤ Pi ≤ 1,000)

www.acmicpc.net

문제 해결 로직

  1. 인출 시간이 작은 사람을 기준으로 정렬한다.
  2. 인출 한 사람의 인출 시간을 더한다.

 

아래와 같이 소스를 공유합니다.

public class ATM {
    public static Comparator<Integer> compare = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            return Integer.compare(o1, o2);
        }
    };
    public static List<Integer> list = new ArrayList<>();
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());

        StringTokenizer st = new StringTokenizer(br.readLine(), " ");

        while(st.hasMoreTokens()){
            int person = Integer.parseInt(st.nextToken());
            list.add(person);
        }

        list.sort(compare);

        int sum = 0;
        int max = 0;
        for (int i = 0; i < list.size(); i++) {
            sum += list.get(i);
            max += sum;
        }

        System.out.println(max);
    }
}

+ Recent posts