문제링크 : https://www.acmicpc.net/problem/10825
Comparable 인터페이스 사용해보기 좋은 문제였다.
import java.io.*;
import java.util.*;
public class Main {
static FastReader scan = new FastReader(); // 입력
static StringBuilder sb = new StringBuilder(); //제출답안
static int N;
static StudentInfo[] scoreList;
static void input() { //입력함수
N = scan.nextInt();
scoreList = new StudentInfo[N];
for(int i =0; i < N; i++) {
scoreList[i] = new StudentInfo();
scoreList[i].name = scan.next();
scoreList[i].korean = scan.nextInt();
scoreList[i].english = scan.nextInt();
scoreList[i].math = scan.nextInt();
}
}
public static void main(String[] args) {
input(); // 입력
Arrays.sort(scoreList);
for(int i = 0; i < N; i++) {
sb.append(scoreList[i].name);
sb.append('\n');
}
System.out.println(sb.toString());
}
static class StudentInfo implements Comparable<StudentInfo> {
String name;
int korean, english, math;
@Override
public int compareTo(StudentInfo studentInfo) {
//국어 내림차순
if(korean != studentInfo.korean) {
return studentInfo.korean - korean;
}
//영어 오름차순
if(english != studentInfo.english) {
return english - studentInfo.english;
}
//수학 내림차순
if(math != studentInfo.math) {
return studentInfo.math - math;
}
//이름 오름차순
return name.compareTo(studentInfo.name);
}
}
//류호석 강사님 템플릿
static class FastReader {
BufferedReader br;
StringTokenizer st;
public FastReader() {
br = new BufferedReader(new InputStreamReader(System.in));
}
public FastReader(String s) throws FileNotFoundException {
br = new BufferedReader(new FileReader(new File(s)));
}
String next() {
while (st == null || !st.hasMoreElements()) {
try {
st = new StringTokenizer(br.readLine());
} catch (IOException e) {
e.printStackTrace();
}
}
return st.nextToken();
}
int nextInt() {
return Integer.parseInt(next());
}
long nextLong() {
return Long.parseLong(next());
}
double nextDouble() {
return Double.parseDouble(next());
}
String nextLine() {
String str = "";
try {
str = br.readLine();
} catch (IOException e) {
e.printStackTrace();
}
return str;
}
}
}
'알고리즘' 카테고리의 다른 글
JAVA 백준1015 수열 정렬(BOJ1015) (0) | 2022.03.13 |
---|---|
JAVA 백준2751 수 정렬하기2(BOJ2751)(cpu 초당 연산속도) (0) | 2022.03.12 |
JAVA 백준2750 수 정렬하기(BOJ2750) (0) | 2022.03.12 |
JAVA 백준2529 부등호(BOJ2529) (0) | 2022.03.12 |
JAVA 백준1987 알파벳(BOJ1987) (0) | 2022.03.12 |