CT

[BOJ] 17단계 심화2

kinggora 2023. 4. 13. 00:19
1 1037 약수

1과 N을 제외한 약수들이 주어졌을 때 N 찾기

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        StringTokenizer tokenizer = new StringTokenizer(reader.readLine());
        int[] divisors = new int[n];
        for(int i = 0; i < n; i++) {
            divisors[i] = Integer.parseInt(tokenizer.nextToken());
        }
        Arrays.sort(divisors);
        System.out.println(divisors[0] * divisors[n-1]);
    }
}

 

2 25192 인사성 밝은 곰곰이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(reader.readLine());
        Set<String> users = new HashSet<>();
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            String str = reader.readLine();
            if(str.equals("ENTER")) {
                users.clear();
                continue;
            }
            if(!users.contains(str)) {
                users.add(str);
                cnt++;
            }
        }
        System.out.println(cnt);
    }
}

 

3 26069 붙임성 좋은 총총이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashSet;
import java.util.Set;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        Set<String> dance = new HashSet<>();
        dance.add("ChongChong");
        for(int i = 0; i < N; i++) {
            String[] split = reader.readLine().split(" ");
            if(dance.contains(split[0])){
                dance.add(split[1]);
            } else if(dance.contains(split[1])) {
                dance.add(split[0]);
            }
        }
        System.out.println(dance.size());
    }
}

*중복 제거를 위해 Set 사용

 

4 2108 통계학
  1. 산술평균 : N개의 수들의 합을 N으로 나눈 값
  2. 중앙값 : N개의 수들을 증가하는 순서로 나열했을 경우 그 중앙에 위치하는 값
  3. 최빈값 : N개의 수들 중 가장 많이 나타나는 값
  4. 범위 : N개의 수들 중 최댓값과 최솟값의 차이
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int N = Integer.parseInt(reader.readLine());
        int[] arr = new int[8001];
        List<Integer> list = new ArrayList<>();
        float sum = 0;
        int max = Integer.MIN_VALUE;
        int min = Integer.MAX_VALUE;
        for(int i = 0; i < N; i++) {
            int num = Integer.parseInt(reader.readLine());
            sum += num;
            if(max < num) {
                max = num;
            }
            if(min > num) {
                min = num;
            }
            arr[num + 4000]++;
            list.add(num);
        }
        //산술 평균: 소수점 이하 첫째 자리에서 반올림
        System.out.println(Math.round(sum/N));
        //중앙값
        Collections.sort(list);
        if(list.size() == 1) {
            System.out.println(list.get(1));
        } else {
            System.out.println(list.get(N/2));
        }

        int frequency = 0;
        
//        for(int i = 0; i < arr.length; i++) {
//            if(arr[i] > 0) {
//                center--;
//                if(center == 0) {
//                    System.out.println(i - 4000);
//                }
//                //if(frequency > )
//            }
//        }
        System.out.println(max - min);
        //합, 정렬, 빈도수, 최댓값-최솟값
    }
}

 

5 20920 영단어 암기는 괴로워
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        String[] split = reader.readLine().split(" ");
        int N = Integer.parseInt(split[0]);
        int M = Integer.parseInt(split[1]);

        Map<String, Integer> words = new HashMap<>();
        for(int i = 0; i < N; i++) {
            String str = reader.readLine();
            if(str.length() >= M) {
                if(words.containsKey(str)){
                    words.put(str, words.get(str) + 1);
                } else {
                    words.put(str, 1);
                }
            }
        }

        List<Map.Entry<String, Integer>> entries = new ArrayList<>(words.entrySet());
        entries.sort(new Comparator<Map.Entry<String, Integer>>() {
            @Override
            public int compare(Map.Entry<String, Integer> o1, Map.Entry<String, Integer> o2) {
                //1. 빈도수 내림차
                if(o1.getValue() != o2.getValue()) {
                    return o2.getValue() - o1.getValue();
                }
                //2. 길이 내림차
                else if(o1.getKey().length() != o2.getKey().length()) {
                    return o2.getKey().length() - o1.getKey().length();
                }
                //3. 사전순
                else {
                    return o1.getKey().compareTo(o2.getKey());
                }
            }
        });

        StringBuilder sb = new StringBuilder();
        for(Map.Entry<String, Integer> entry : entries) {
            sb.append(entry.getKey()).append("\n");
        }
        System.out.print(sb);
    }
}

*key 기준 정렬과 value 기준 정렬이 모두 필요하기 때문에 Map.Entry 구조 사용