문제(출처: https://www.acmicpc.net/problem/5741)
< Soccer League >
문제 풀이
이기면 +3점
무승부 +1점
점수 내림차순으로 정렬, 점수가 동일하다면 득실차 내림차순으로 정렬
my solution (Java)
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.StringTokenizer;
public class _5741_ { // Soccer League
public static void main(String[] args) throws IOException {
BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
StringTokenizer st;
int n = 0;
while ((n = Integer.parseInt(bf.readLine())) != 0) {
HashMap<String, int[]> map = new HashMap<>();
for (int i = 0; i < n; i++) {
st = new StringTokenizer(bf.readLine());
String club1 = st.nextToken();
int x = Integer.parseInt(st.nextToken());
st.nextToken();
int y = Integer.parseInt(st.nextToken());
String club2 = st.nextToken();
if (!map.containsKey(club1)) {
map.put(club1, new int[] { 0, 0 });
}
if (!map.containsKey(club2)) {
map.put(club2, new int[] { 0, 0 });
}
if (x > y) {
map.get(club1)[0] += 3;
map.get(club1)[1] += (x - y);
map.get(club2)[1] += (y - x);
} else if (x < y) {
map.get(club2)[0] += 3;
map.get(club1)[1] += (x - y);
map.get(club2)[1] += (y - x);
} else {
map.get(club1)[0] += 1;
map.get(club2)[0] += 1;
}
}
ArrayList<String> result = new ArrayList<>(map.keySet());
Collections.sort(result, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
if (map.get(o2)[0] == map.get(o1)[0]) {
return map.get(o2)[1] - map.get(o1)[1];
}
return map.get(o2)[0] - map.get(o1)[0];
}
});
for (String club : result) {
bw.write(map.get(club)[0] + " " + club + "\n");
}
bw.write("\n");
}
bw.flush();
}
}
변수)
n : 경기 수
map : HashMap <팀 이름, [점수, 득실차]>
club1, club2, x, y : 팀 이름, 득점한 골 수
result : HashMap key -> ArrayList
경기 수가 0이 아닐 때까지 입력받아 다음 과정을 반복한다.
1) 경기 수만큼 다음 과정을 반복한다.
1-1) 각 팀 이름과 득점한 골 수를 입력받는다
1-2) HashMap에 key값으로 팀 이름이 없다면 추가한다.
1-3) 이긴 팀은 3점, 비겼으면 둘 다 1점을 추가하고 득실차를 더한다.
2) HashMap의 key값을 ArrayList로 변환 후 총점 기준 내림차순, 총점이 같다면 득실차 기준 내림차순으로 정렬한다.
최종 ArrayList를 출력한다.

'🌞Algorithm > 🔥Baekjoon' 카테고리의 다른 글
| [Baekjoon] 29882_Ranking (0) | 2025.11.04 |
|---|---|
| [Baekjoon] 21149_Unread Messages (0) | 2025.11.03 |
| [Baekjoon] 9794_Another Word Sorting (0) | 2025.10.30 |
| [Baekjoon] 4676_Haiku Review (0) | 2025.10.29 |
| [Baekjoon] 5747_Odd or Even (0) | 2025.10.27 |