[BOJ 10825] 감시피하기

업데이트:

문제

  • BOJ 10825
  • 문제의 저작권은 Baekjoon Online Judge에 있습니다.

접근방식

학생 클래스를 만들어서 Comparable로 정렬 기준을 정해주었다.
현재 객체 a와 비교 객체b가 있을 때, compareTo(a, b) 의 결과는 a<b일때 음수, a>b일때 양수를 반환한다.
따라서 이를 이용해 오름차순인 경우 음수를 리턴, 내림차순인 경우 순서를 바꿔줘야 하므로 양수를 리턴해주면 된다.

import java.io.*;
import java.util.*;

public class Main {

	static BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
	static StringTokenizer st;
	static int N;
	static List<Student> list;
	static StringBuilder sb;

	static class Student implements Comparable<Student> {
		String name;
		int kor, eng, math;

		public Student(String name, int kor, int eng, int math) {
			this.name = name;
			this.kor = kor;
			this.eng = eng;
			this.math = math;
		}

		@Override
		public int compareTo(Student o) {
			if (this.kor == o.kor) {
				if (this.eng == o.eng) {
					if (this.math == o.math) {
						// 4. 모든 점수가 같으면 이름이 사전 순으로 증가하는 순서로
						return this.name.compareTo(o.name);
					} else { // 3. 국어 점수와 영어 점수가 같으면 수학 점수가 감소하는 순서로
						return o.math - this.math;
					}
				} else { // 2. 국어 점수가 같으면 영어 점수가 증가하는 순서로
					return this.eng - o.eng;
				}
			} else { // 1. 국어 점수가 감소하는 순으로
				return o.kor - this.kor;
			}
		}
	}

	public static void main(String[] args) throws IOException {
		N = Integer.parseInt(br.readLine());
		list = new ArrayList<>();

		for (int n = 0; n < N; n++) {
			st = new StringTokenizer(br.readLine());
			list.add(new Student(st.nextToken(), Integer.parseInt(st.nextToken()), Integer.parseInt(st.nextToken()),
					Integer.parseInt(st.nextToken())));
		} // End input

		Collections.sort(list);

		sb = new StringBuilder();
		for (Student s : list) {
			sb.append(s.name + "\n");
		}
		System.out.println(sb.toString());
	}
}

태그:

카테고리:

업데이트: