23년 7월 22일 [알고리즘 - 정렬]

sua·2023년 7월 22일
0

알고리즘 가보자고

목록 보기
61/101

백준 10825번 국영수

문제


나의 풀이

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

public class Main {
    static class Person implements Comparable<Person> {
        String name;
        int kor, eng, math;
        Person(String name, int kor, int eng, int math) {
            this.name = name;
            this.kor = kor;
            this.eng = eng;
            this.math = math;
        }
        public int compareTo(Person that) {
            if(this.kor > that.kor) { // 내림차순
                return -1;
            } else if(this.kor == that.kor) {
                if(this.eng < that.eng) { // 오름차순
                    return -1;
                }  else if(this.eng == that.eng) {
                    if(this.math > that.math) { // 내림차순
                        return -1;
                    } else if(this.math == that.math) {
                        return this.name.compareTo(that.name); // 사전순
                    }
                }
            }
            return 1;
        }
    }
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        Person a[] = new Person[n];
        for(int i = 0; i < n; i++) {
            String line[] = br.readLine().split(" ");
            a[i] = new Person(line[0], Integer.parseInt(line[1]), Integer.parseInt(line[2]), Integer.parseInt(line[3]));
        }
        Arrays.sort(a);
        StringBuilder sb = new StringBuilder();
        for(int i = 0; i < n; i++) {
            sb.append(a[i].name + "\n");
        }
        System.out.print(sb);
    }
}

비교 함수에서 4가지에 대해서 비교를 해주면 된다.

결과


백준 10989번 수 정렬하기 3

문제


나의 풀이

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

public class Main {
    public static void main(String args[]) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        int[] count = new int[10001];
        for(int i = 0; i < n; i++) {
            int temp = Integer.parseInt(br.readLine());
            count[temp] += 1;
        }
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
        for(int i = 1; i <= 10000; i++) {
            if(count[i] > 0) {
                for(int j = 0; j < count[i]; j++) {
                    bw.write(i + "\n");
                }
            }
        }
        bw.flush();
    }
}

결과


백준 11652번 카드

문제


나의 풀이

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

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int n = Integer.parseInt(br.readLine());
        long a[] = new long[n];
        for(int i = 0; i < n; i++) {
            a[i] = Long.parseLong(br.readLine());
        }
        Arrays.sort(a);
        long answer = a[0];
        int answer_count = 1;
        int count = 1;
        for(int i = 1; i < n; i++) {
            if(a[i] == a[i - 1]) {
                count += 1;
            } else {
                count = 1;
            }
            if(answer_count < count) {
                answer_count = count;
                answer = a[i];
            }
        }
        System.out.println(answer);
    }
}

정렬을 시킨 뒤 앞의 수와 현재 수가 같으면 개수를 증가시켜서 연속된 수가 몇개인지 구하고 그중에서 가장 큰 값을 정답으로 출력한다.

결과

profile
가보자고

1개의 댓글

comment-user-thumbnail
2023년 7월 22일

풀이 과정이 정말 깔끔하게 잘 설명되어 있어서 쉽게 이해할 수 있었습니다. 앞으로도 좋은 포스트 기대할게요!

답글 달기