๋ฌธ์ (์ถ์ฒ: https://www.acmicpc.net/problem/9787)
< Olympic Games Ranking >
๋ฌธ์ ํ์ด
๋ค์ ๊ธฐ์ค์ผ๋ก ์ ๋ ฌํ์ฌ ์ถ๋ ฅํ๋ค.
1) ๊ธ๋ฉ๋ฌ ์
2) ์๋ฉ๋ฌ ์
3) ๋๋ฉ๋ฌ ์
4) ์ํ๋ฒณ ์
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.StringTokenizer;
public class _9787_ { // Olympic Games Ranking
static class Nation implements Comparable<Nation> {
String country;
int gold;
int silver;
int bronze;
int sum;
public Nation(String country, int gold, int silver, int bronze) {
this.country = country;
this.gold = gold;
this.silver = silver;
this.bronze = bronze;
this.sum = gold + silver + bronze;
}
@Override
public int compareTo(Nation o) {
if (o.gold == this.gold) {
if (o.silver == this.silver) {
if (o.bronze == this.bronze) {
return this.country.compareTo(o.country);
}
return o.bronze - this.bronze;
}
return o.silver - this.silver;
}
return o.gold - this.gold;
}
}
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;
String str = "";
ArrayList<Nation> list = new ArrayList<>();
while ((str = bf.readLine()) != null && !str.equals("")) {
st = new StringTokenizer(str);
list.add(new Nation(st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
Integer.parseInt(st.nextToken())));
}
Collections.sort(list);
int idx = 1;
for (int i = 0; i < list.size(); i++) {
if (i > 0 && !(list.get(i - 1).gold == list.get(i).gold && list.get(i - 1).silver == list.get(i).silver
&& list.get(i - 1).bronze == list.get(i).bronze)) {
idx = i + 1;
}
bw.write(idx + " " + list.get(i).country + " " + list.get(i).gold + " " + list.get(i).silver + " "
+ list.get(i).bronze + " " + list.get(i).sum + "\n");
}
bw.flush();
}
}
๋ณ์)
str : ์ ๋ ฅ๊ฐ
list : ArrayList <Nation>
idx : ์์
Nation
๊ตญ๊ฐ ์ด๋ฆ, ๊ธ๋ฉ๋ฌ ์, ์๋ฉ๋ฌ ์, ๋๋ฉ๋ฌ ์, ์ด ๋ฉ๋ฌ ์๋ฅผ ๋ณ์๋ก ๊ฐ์ง๋ค. ์ ๋ ฌ ์กฐ๊ฑด์ ๊ธ๋ฉ๋ฌ ์, ๊ธ๋ฉ๋ฌ ์๊ฐ ๊ฐ๋ค๋ฉด ์๋ฉ๋ฌ ์, ์๋ฉ๋ฌ ์๊ฐ ๊ฐ๋ค๋ฉด ๋๋ฉ๋ฌ ์, ๋๋ฉ๋ฌ ์๊ฐ ๊ฐ๋ค๋ฉด ๊ตญ๊ฐ ์ํ๋ฒณ ์์ผ๋ก ์ ๋ ฌํ๋ค.
Main
์ ๋ ฅ์ด ์์ ๋๊น์ง ๊ตญ๊ฐ ์ ๋ณด๋ฅผ ์ ๋ ฅ๋ฐ์ ArrayList์ ์ ์ฅํ๋ค. ์ ๋ ฌ ํ ์์๋๋ก ์ถ๋ ฅํ๋ค. ์ด๋, ์ ๊ตญ๊ฐ์ ๋ชจ๋ ๋ฉ๋ฌ ์๊ฐ ๊ฐ์์ง ํ์ธ ํ ์์๋ฅผ ๊ฒฐ์ ํ๋ค.
'๐Algorithm > ๐ฅBaekjoon' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Baekjoon] 9609_PSU Campuses (1) | 2025.08.11 |
---|---|
[Baekjoon] 3443_Reaux! Sham! Beaux! (4) | 2025.07.30 |
[Baekjoon] 9491_Politics (2) | 2025.07.28 |
[Baekjoon] 15198_NKD (2) | 2025.07.25 |
[Baekjoon] 10442_Rank Order (1) | 2025.07.24 |