boj - 10825 국영수(자바)

SlowAnd·2024년 1월 1일
0

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

class Student{
            String name;
            int kor;
            int eng;
            int math;

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

            public String getName() {
                return name;
            }

            public int getKor() {
                return kor;
            }

            public int getEng() {
                return eng;
            }

            public int getMath() {
                return math;
            }

            @Override
            public String toString() {
                return "Student{" +
                        "name='" + name + '\'' +
                        ", kor=" + kor +
                        ", eng=" + eng +
                        ", math=" + math +
                        '}';
            }
        }

1. 초기화후 + 람다스트림(정렬+출력)

package boj;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class boj_10825 {
    public static void main(String[] args) throws IOException {
       
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(r.readLine());
        List<Student> students = new ArrayList<>();
        for(int i= 0; i<count ; i++){
            List<String> list = Arrays.stream(r.readLine().split(" ")).collect(Collectors.toList());
            students.add(new Student(
                    list.get(0)
                    ,Integer.parseInt(list.get(1))
                    ,Integer.parseInt(list.get(2))
                    ,Integer.parseInt(list.get(3))
            ));
        }
        
        students.stream()
                .sorted(Comparator.comparing(Student::getKor).reversed() // 국어 점수 내림차순
                        .thenComparing(Student::getEng) // 영어 점수 오름차순
                        .thenComparing(Comparator.comparing(Student::getMath).reversed()) // 수학 점수 내림차순
                        .thenComparing(Student::getName)) // 이름 오름차순
                .forEach(student -> System.out.println(student.getName()));



    }
}

2.람다 스트림(초기화 + 정렬+ 출력)

public class boj_10825 {
    public static void main(String[] args) throws IOException {
        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        int count = Integer.parseInt(r.readLine());

        IntStream.range(0, count)
                .mapToObj(i -> {
                    try {
                        String[] list = r.readLine().split(" ");
                        return new Student(list[0], Integer.parseInt(list[1]), Integer.parseInt(list[2]),
                                Integer.parseInt(list[3]));
                    } catch (IOException e) {
                        throw new RuntimeException(e);
                    }
                })
                .sorted(Comparator.comparing(Student::getKor).reversed()
                        .thenComparing(Student::getEng)
                        .thenComparing(Comparator.comparing(Student::getMath).reversed())
                        .thenComparing(Student::getName))
                .forEach(student -> System.out.println(student.getName()));
    

    }

    }
}

3. 최적화 람다 스트림

package boj;

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.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class boj_10825 {
    public static void main(String[] args) throws IOException {
        class Student{
            String name;
            int kor;
            int eng;
            int math;

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

            public String getName() {
                return name;
            }

            public int getKor() {
                return kor;
            }

            public int getEng() {
                return eng;
            }

            public int getMath() {
                return math;
            }


        }

        BufferedReader r = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter w = new BufferedWriter(new OutputStreamWriter(System.out));
        int count = Integer.parseInt(r.readLine());
        List<Student> students = new ArrayList<>();
        for(int i= 0; i<count ; i++){
            List<String> list = Arrays.stream(r.readLine().split(" ")).collect(Collectors.toList());
            students.add(new Student(
                    list.get(0)
                    ,Integer.parseInt(list.get(1))
                    ,Integer.parseInt(list.get(2))
                    ,Integer.parseInt(list.get(3))
            ));
        }

        String result = students.stream()
                .sorted(Comparator.comparing(Student::getKor).reversed() // 국어 점수 내림차순
                        .thenComparing(Student::getEng) // 영어 점수 오름차순
                        .thenComparing(Comparator.comparing(Student::getMath).reversed()) // 수학 점수 내림차순
                        .thenComparing(Student::getName)) // 이름 오름차순
                .map(Student::getName)
                .collect(Collectors.joining("\n"));
//                .forEach(student -> System.out.println(student.getName()));


        w.write(result);
        w.flush();
    }
}

0개의 댓글