-
백준 1296 - 팀 이름 정하기(JAVA)Algorithm 2022. 9. 27. 09:52728x90
오늘 풀어본 문제는 백준 1296번 팀 이름 정하기 문제이다.
브론즈 1 치고는 변수가 상당히 복잡하다. 변수가 생각보다 많이 등장해서 꽤나 당황 스러웠다.
일단, 연두 이름 변수, 몇 개의 팀을 입력받을지 정하는 N 변수, 팀 이름을 담을 배열 변수, 우승 확률을 담을 배열 변수 이정도가 큼지막한 변수들이다.
이제 내가 생각한 알고리즘을 소개하겠다.
※ 알고리즘
1. 연두 이름 변수, 몇 개의 팀을 입력받을지 정하는 N 변수, 팀 이름을 담을 배열 변수, 우승 확률을 담을 배열 변수를 선언한다.
2. 변수 선언 후 팀 이름을 담을 배열 변수를 for 반복문을 이용하여 N개 만큼 입력받는다.
3. 우승확률을 계산할 함수를 하나 만든다. -> 함수의 로직은 charAt으로 String으로 받은 이름들을 쪼개어서 L,O,V,E가 나올때마다 각 문자의 개수를 +1 시켜주는 방식으로 if ~ else if문을 사용하낟.
4. main 함수에서 우승 확률을 담을 배열 변수에 우승확률을 계산한 함수와 for문을 통해 조사한 max 값을 찾아 담아준다.
5. 만약 max 값과 우승 확률이 같아지면 그대로 해당 팀 이름을 출력시켜 주며 반복문을 break 해준다.
알고리즘을 그대로 생각해서 짠 코드이다. 여기서 내가 처음에 실수한 부분이 있는데 바로 Arrays.sort(teamName) 부분이다. 오름차순으로 sort를 시켜주어야 max 한 값을 찾는데 효율적이기 때문에 저 부분을 놓치면 백준에서는 틀린 부분으로 간주된다. 앞으로는 어떤 조사를 해야 할 경우 배열안에서는 sort 시키는 습관을 기르도록 하자.
728x90'Algorithm' 카테고리의 다른 글
백준 2775 - 부녀회장이 될테야(JAVA) (0) 2022.09.28 백준 10769 - 행복한지 슬픈지(JAVA) (0) 2022.09.28 백준 11050 - 이항 계수1(JAVA) (1) 2022.09.25 백준 14467 - 소가 길을 건너간 이유(JAVA) (0) 2022.09.25 백준 2902 - KMP는 왜 KMP일까? (JAVA) (0) 2022.09.20