티스토리 뷰

Algorithms/BOJ

백준 Q.1159 농구 경기

PeonyF 2020. 1. 8. 05:43
반응형

문제

상근이는 농구의 세계에서 점차 영향력을 넓혀가고 있다. 처음에 그는 농구 경기를 좋아하는 사람이었다. 농구에 대한 열정은 그를 막을 수 없었고, 결국 상근이는 농구장을 청소하는 일을 시작했다. 상근이도 농구장을 청소하면서 감독이 되기 위해 가져야할 능력을 공부해나갔다. 서당개 3년이면 풍월을 읊듯이 상근이는 점점 감독으로 한 걸음 다가가고 있었다. 어느 날 그에게 지방의 한 프로농구팀을 감독할 기회가 생기게 되었다. 그는 엄청난 지도력을 보여주며 프로 리그에서 우승을 했고, 이제 국가대표팀의 감독이 되었다.

내일은 일본과 국가대표 친선 경기가 있는 날이다. 상근이는 내일 경기에 나설 선발 명단을 작성해야 한다.

국가대표팀의 감독이 된 이후에 상근이는 매우 게을러졌다. 그는 선수의 이름을 기억하지 못하고, 각 선수의 능력도 알지 못한다. 따라서, 누가 선발인지 기억하기 쉽게 하기 위해 성의 첫 글자가 같은 선수 5명을 선발하려고 한다. 만약, 성의 첫 글자가 같은 선수가 5명보다 적다면, 상근이는 내일 있을 친선 경기를 기권하려고 한다.

상근이는 내일 경기를 위해 뽑을 수 있는 성의 첫 글자를 모두 구해보려고 한다.

입력

첫째 줄에 선수의 수 N (1 ≤ N ≤ 150)이 주어진다. 다음 N개 줄에는 각 선수의 성이 주어진다. (성은 알파벳 소문자로만 이루어져 있고, 최대 30글자이다)

출력

상근이가 선수 다섯 명을 선발할 수 없는 경우에는 "PREDAJA" (따옴표 없이)를 출력한다. PREDAJA는 크로아티아어로 항복을 의미한다. 선발할 수 있는 경우에는 가능한 성의 첫 글자를 사전순으로 공백없이 모두 출력한다.

 

Java Solution

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;


public class Main {

    public static void main(String[] args) throws IOException {

        BaseballMatch baseballMatch = new BaseballMatch();

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int countPlayers = Integer.parseInt(br.readLine());

        for (int i = 0; i < countPlayers; i++) {
            String nameOfPlayer = br.readLine();

            baseballMatch.splitFirstLetterFromPlayerName(nameOfPlayer);
            baseballMatch.countFirstLetterFromPlayerName();
        }

        baseballMatch.getForEachFirstLetter();
        String firstLetterOfSelectedPlayerName = baseballMatch.print5PlayersWhoHaveSameFirstLetter();
        System.out.println(firstLetterOfSelectedPlayerName);
    }
}

class BaseballMatch {


    private static final int firstLetterIndex = 0;
    private static final int baseNumPlayer = 1;
    private static final int selectedPlayersNum = 5;
    private static final String surrender = "PREDAJA";

    private Map<Character, Integer> mapOfPlayersName = new HashMap<>();

    private char firstLetterOfName;
    private String selectedPlayer = "";


    void splitFirstLetterFromPlayerName(String nameOfPlayer) {
        firstLetterOfName = nameOfPlayer.charAt(firstLetterIndex);
    }

    void countFirstLetterFromPlayerName() {
        if (flagContainsFirstLetter(firstLetterOfName)) {
            int countPlayer = mapOfPlayersName.get(firstLetterOfName);
            mapOfPlayersName.put(firstLetterOfName, baseNumPlayer + countPlayer);
        }

        if (!flagContainsFirstLetter(firstLetterOfName)) {
            mapOfPlayersName.put(firstLetterOfName, baseNumPlayer);
        }
    }

    private boolean flagContainsFirstLetter(char firstLetterOfName) {
        return mapOfPlayersName.containsKey(firstLetterOfName);
    }

    void getForEachFirstLetter() {
        for (char selectedPlayerTmpKey : mapOfPlayersName.keySet()) {
            int selectedPlayerTmpValue = mapOfPlayersName.get(selectedPlayerTmpKey );
            select5PlayersWhoHaveSameFirstLetter(selectedPlayerTmpKey  , selectedPlayerTmpValue );
        }
    }

    private void select5PlayersWhoHaveSameFirstLetter(char selectedPlayerTmpKey , int selectedPlayerTmpValue ) {
        if (selectedPlayerTmpValue >= selectedPlayersNum) {
            selectedPlayer += selectedPlayerTmpKey ;
        }
    }

    String print5PlayersWhoHaveSameFirstLetter() {
        if ("".equals(selectedPlayer)) {
            return surrender;
        }
        return selectedPlayer;
    }
}

 

Q.HashMap의 Iterator 사용법

Iterator을 사용하여 구현시
For-Each문 을 사용하여 Hashmap 저장 

반응형

'Algorithms > BOJ' 카테고리의 다른 글

Q10972 다음순열  (0) 2020.03.06
백준 Q.1157 단어 공부  (0) 2020.01.08
백준 Q.1302 베스트셀러  (0) 2020.01.07
백준 Q.9375 패션왕 신해빈  (0) 2020.01.06
백준 Q.10814 나이 순 정렬하기  (0) 2020.01.03
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2024/04   »
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
글 보관함