๊ธฐ๋ก๋ฐฉ

BOJ_10825 : ๊ตญ์˜์ˆ˜ ๋ณธ๋ฌธ

CodingTest/Java

BOJ_10825 : ๊ตญ์˜์ˆ˜

Soom_1n 2022. 11. 10. 12:54

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

10825๋ฒˆ: ๊ตญ์˜์ˆ˜

์ฒซ์งธ ์ค„์— ๋„ํ˜„์ด๋„ค ๋ฐ˜์˜ ํ•™์ƒ์˜ ์ˆ˜ N (1 ≤ N ≤ 100,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ํ•œ ์ค„์— ํ•˜๋‚˜์”ฉ ๊ฐ ํ•™์ƒ์˜ ์ด๋ฆ„, ๊ตญ์–ด, ์˜์–ด, ์ˆ˜ํ•™ ์ ์ˆ˜๊ฐ€ ๊ณต๋ฐฑ์œผ๋กœ ๊ตฌ๋ถ„ํ•ด ์ฃผ์–ด์ง„๋‹ค. ์ ์ˆ˜๋Š” 1๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 1

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์ด๋ฆ„, ๊ตญ์–ด, ์˜์–ด ์ˆ˜ํ•™์„ ์ž…๋ ฅ๋ฐ›๊ณ  ๋ฌธ์ œ์— ์ œ์‹œ๋œ ๋Œ€๋กœ ์ •๋ ฌ ํ›„ ์ด๋ฆ„์„ ์ถœ๋ ฅํ•œ๋‹ค.
  • 4๊ฐ€์ง€ ์กฐ๊ฑด์— ๋”ฐ๋ฅธ ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ์ด ํ•„์š”ํ•˜๋‹ค.
    • ์ •๋ ฌ์„ ์œ„ํ•œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  Comparable์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•ด compareTo ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        StringBuilder sb = new StringBuilder();
        int n = Integer.parseInt(br.readLine());
        Mydata[] arr = new Mydata[n];

        for (int i = 0; i < n; i++){
            StringTokenizer st = new StringTokenizer(br.readLine());
            arr[i] = new Mydata(st.nextToken(), Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()),Integer.parseInt(st.nextToken()));
        }

        Arrays.sort(arr);

        for (int i = 0; i < n; i++) {
            System.out.println(arr[i].name);
        }
    }
}

class Mydata implements Comparable<Mydata> {
    String name;
    int kor;
    int eng;
    int math;

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

    @Override
    public int compareTo(Mydata o) {
        if (o.kor != this.kor)
            return o.kor - this.kor;
        else if (o.eng != this.eng)
            return this.eng - o.eng;
        else if (o.math != this.math)
            return o.math - this.math;
        else return this.name.compareTo(o.name);
    }
}

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ์„ ์œ„ํ•ด ์ƒˆ ํด๋ž˜์Šค Mydata๋ฅผ ๋งŒ๋“ค๊ณ , 4๊ฐ€์ง€ ๋ฐ์ดํ„ฐ๋ฅผ ์ž…๋ ฅ๋ฐ›์•„ ์ €์žฅํ•œ๋‹ค.
  • Mydata๋Š” Comparable ์ธํ„ฐํŽ˜์ด์Šค๋ฅผ ์ƒ์†ํ•˜๊ธฐ ๋•Œ๋ฌธ์— compareTo ๋ฉ”์†Œ๋“œ๋ฅผ ์˜ค๋ฒ„๋ผ์ด๋“œ ํ•ด์•ผํ•œ๋‹ค.
    • compareTo ๋ฉ”์†Œ๋“œ์—์„œ ์ •๋ ฌ ์กฐ๊ฑด์„ ๋ฌธ์ œ์— ๋งž๊ฒŒ ์กฐ์ •ํ•œ๋‹ค.
    • ์Œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ํ˜„์žฌ ๊ฐ์ฒด๊ฐ€ ๋” ๋น ๋ฅด๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
      • 0์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๊ฐ™์€ ๊ฐ’์ด๋‹ค.
      • ์–‘์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๋ฉด ๋น„๊ตํ•˜๋Š” ๊ฐ์ฒด๊ฐ€ ๋” ๋น ๋ฅด๋‹ค๋Š” ์˜๋ฏธ์ด๋‹ค.
    • ๋ฌธ์ž์—ด์€ compareTo ๋ฉ”์†Œ๋“œ๋ฅผ ๋”ฐ๋กœ ์ด์šฉํ•˜๋Š”๋ฐ, ๊ฐ™์€ ์˜๋ฏธ๋กœ ์•„์Šคํ‚ค ์ฝ”๋“œ๊ฐ€ ๋” ์ž‘์œผ๋ฉด (์‚ฌ์ „์ˆœ ์•ž์ด๋ฉด) ์Œ์ˆ˜๋ฅผ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์‚ฌ์šฉ์ž ์ง€์ • ์ •๋ ฌ์„ ์ถฉ๋ถ„ํžˆ ์—ฐ์Šตํ•ด ๋ณผ ์ˆ˜ ์žˆ์—ˆ๋˜ ์ข‹์€ ๋ฌธ์ œ์˜€๋‹ค.
  • ์ด์ œ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค๊ณ  ์ •๋ ฌ ์กฐ๊ฑด์„ ๋ฐ”๊ฟ”์ฃผ๋Š”๊ฑด ์ต์ˆ™ํ•ด ์ง„ ๊ฒƒ ๊ฐ™์•„์„œ ๋ฟŒ๋“ฏํ•˜๋‹ค.

728x90