[코드트리] 코딩톡

h_jin·2025년 1월 10일

코테

목록 보기
7/33

문제링크

문제

n명의 사람이 m개의 메세지를 보냈다
누가 보낸 메세지를 몇명이 안읽었는지 m개가 나와있고
p번째 메세지를 안 읽은 사람을 출력
단 사람은 1번부터 A, B, C, D,,,,

틀린 풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        sc.nextLine();

        int[] lst = new int[26];
        char[] how = new char[100];

        for (int i = 0; i < m; i++){
            String tmp = sc.nextLine().trim();
            lst[(int)(tmp.charAt(0) - 'A')] = Integer.parseInt(tmp.substring(2));
            how[i] = tmp.charAt(0);
        }

        if (lst[(int)(how[p - 1] - 'A')] == 0){
            System.out.print("");
            return ;
        }

        int[] read = new int[n];
        for (int i = 0 ; i < p - 1; i++){
            if (lst[(int)(how[i] - 'A')] == lst[(int)(how[p - 1] - 'A')]){
                read[(int)(how[i] - 'A')]++;
            }
        }
        for (int i = p - 1; i < m; i++)
            read[(int)(how[i] - 'A')]++;
        
        for (int i = 0; i < n; i++){
            if (read[i] == 0){
                char tmp = (char)('A' + i);
                System.out.print(tmp + " ");
            }
        }
    }
}
  • 특정 사람이 보낸 메세지를 몇명이 읽었는지 저장 (가장 최근 것)
  • 누가 몇번째에 메세지를 보냈는가 저장
  • 한 사람이 여러번 메세지를 다른 시간에 보내면 원하는 결과를 찾을 수 없음 (읽지 않은 사람의 수가 다른 경우)

풀이

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();
        int m = sc.nextInt();
        int p = sc.nextInt();
        sc.nextLine();

        String[] lst = new String[m];

        for (int i = 0; i < m; i++)
            lst[i] = sc.nextLine().trim();
        
        int[] read = new int[26];

        int p_person = Integer.parseInt(lst[p - 1].substring(2));

        for (int i = 0; i < p - 1; i++){
            if (Integer.parseInt(lst[i].substring(2)) == p_person)
                read[(int)(lst[i].charAt(0) - 'A')]++;
        }
        for (int i = p - 1; i < m; i++)
            read[(int)(lst[i].charAt(0) - 'A')]++;


        for (int i = 0; i < n; i++){
            if (p_person == 0){
                System.out.print("");
                break;
            }
            if (read[i] == 0)
                System.out.print((char)('A' + i) + " ");
        }
    }
}
  • m개의 메세지 관련 정보가 A 0 식으로 들어오기 때문에 String으로 받아줌
  • 확인하고 싶은 메세지 이후에 메세지를 보낸 사람들은 읽었다고 판단.
  • 그 전에 보낸 메세지이나, 읽은 사람의 수가 동일하면 p번째 메세지도 읽었다고 판단.
  • 읽지 않은 수가 0이면 모두 읽었다고 판단 (안 읽은 사람 X)

0개의 댓글