[백준] 10825번:국영수 (Java 자바)

[백준] 10825번:국영수 (Java 자바)

728x90

문제

https://www.acmicpc.net/problem/10825

풀이 및 소스코드

우선순위큐로 풀면 되는 문제이다.

자바에서는 우선순위큐를 풀기위해서는 Comparable 이라는 인터페이스를 implements 하면 된다.

메소드에 원하는 정렬을 정의하면 되는데,

this.mat : 인덱스가 앞서는 원소, o.mat : 인덱스가 뒤에있는 원소라고 생각하면 된다.

앞에꺼 빼기 뒤에꺼 즉, this.mat - o.mat 이 양수면 둘이 뒤집힌다. 즉 앞에있는 것이 클 때 ( ex. 6과 4 ) 뒤집힌다는 소리는 오름차순이라고 생각하면 된다.

this.mat - o.mat 이 음수면 가만히 있는다.

정리 : comparable 사용시, 앞-뒤 계산 후 결과 값이 양수면 오름차순정렬, 음수면 내림차순정렬 !

import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.PriorityQueue; import java.util.StringTokenizer; public class Main { public static void main(String[] args) throws NumberFormatException, IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); StringTokenizer st; StringBuilder sb = new StringBuilder(); PriorityQueue q = new PriorityQueue(); int n = Integer.parseInt(br.readLine()); for(int i=0;i

"); } System.out.println(sb); } } class student implements Comparable{ int kor, eng, mat; String name; @Override public int compareTo(student o) { if(this.kor == o.kor) { // 국어점수가 같으면 if(this.eng == o.eng) { // 영어점수도 같으면 if(this.mat==o.mat) { // 모든 점수가 같으면 return this.name.compareTo(o.name); } // 수학점수가 감소하는 순서로 return o.mat-this.mat; } // 영어 점수가 증가하는 순서로 return this.eng-o.eng; } // 국어 점수가 감소하는 순서로 return o.kor-this.kor; } public student(int kor, int eng, int mat, String name) { super(); this.kor = kor; this.eng = eng; this.mat = mat; this.name = name; } }

반응형

from http://jainn.tistory.com/319 by ccl(A) rewrite - 2021-11-12 00:27:44