매일 Algorithm

신재원·2023년 7월 1일
0

Algorithm

목록 보기
157/243

백준 2075번

import java.util.Collections;
import java.util.PriorityQueue;
import java.util.Scanner;

public class problem497 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        // 우선순위 큐는 기본적으로 오름차순
        PriorityQueue<Integer> pq 
        		= new PriorityQueue<>(Collections.reverseOrder());

        int number = in.nextInt();

        // 큐에 값 저장
        for (int i = 0; i < number; i++) {
            for (int j = 0; j < number; j++) {
                pq.offer(in.nextInt());
            }
        }

        // number 이전까지 값을 꺼내서 버린다.
        for (int i = 0; i < number - 1; i++) {
            pq.poll();
        }
        System.out.println(pq.poll());
    }
}

백준 4358번

import java.util.*;

public class problem498 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        Map<String, Integer> map = new HashMap<>();

        int count = 0;
        while (in.hasNextLine()) {
            String str = in.nextLine();
            map.put(str, map.getOrDefault(str, 0) + 1);
            count++;
        }

		// List 배열에 map 객체의 key 값을 담는다.
        List<String> list = new ArrayList<>(map.keySet());


        Collections.sort(list);
        StringBuilder output = new StringBuilder();
        for (String result : list) {
            int total = map.get(result) * 100;
            double value = (double) total / (double) count;
            output.append(result + " "
                    + String.format("%.4f", value)).append("\n");
        }

        System.out.println(output.toString());
    }
}

0개의 댓글