매일 Algorithm

신재원·2023년 6월 23일
0

Algorithm

목록 보기
151/243

백준 8979번

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Map;

public class problem484 {
    public static void main(String[] args) throws IOException {
        BufferedReader br = 
        		new BufferedReader(new InputStreamReader(System.in));
                
        // 메달 배열을 받기위해 value 값에 int[]
        Map<Integer, int[]> medalMap = new HashMap<>(); 
        String[] input = br.readLine().split(" ");

        int size = Integer.parseInt(input[0]);
        int find = Integer.parseInt(input[1]);

        // 국가 메달 정보 입력받아 Map에 저장
        for (int i = 1; i <= size; i++) {
            input = br.readLine().split(" ");
            int[] medals = new int[3];

            for (int j = 0; j < medals.length; j++) {
                medals[j] = Integer.parseInt(input[j + 1]);
            }

            medalMap.put(Integer.parseInt(input[0]), medals);
        }

        int[] target = medalMap.get(find);
        int rank = 1;

        // 다른 국가들과 비교하여 등수 결정
        for (int i = 1; i <= size; i++) {
            // 자기 자신과는 비교하지 않음
            if (i == find) {
                continue;
            }

            int[] temp = medalMap.get(i);

            // 금메달 수 비교
            if (temp[0] > target[0]) {
                rank++;
            } else if (temp[0] == target[0]) {
                // 은메달 수 비교
                if (temp[1] > target[1]) {
                    rank++;
                } else if (temp[1] == target[1]) {
                    // 동메달 수 비교
                    if (temp[2] > target[2]) {
                        rank++;
                    }
                }
            }
        }

        System.out.println(rank);
    }
}

백준 9324번

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class problem485 {
    public static void main(String[] args) throws IOException {
        BufferedReader br
                = new BufferedReader(new InputStreamReader(System.in));

        int size = Integer.parseInt(br.readLine());

        for (int i = 0; i < size; i++) {
            int[] alpha = new int[26];
            String str = br.readLine();
            String msg = "OK";
            boolean flag = false;
            for (int j = 0; j < str.length(); j++) {
                // 입력값 ABAAAB 인경우 4번째 A는 건너 뛰어야합니다.
                if (flag) {
                    flag = false;
                    continue;
                }
                alpha[str.charAt(j) - 'A']++; // 알파벳 증가

                // 똑같은 알파벳이 3개 있는경우
                if (alpha[str.charAt(j) - 'A'] == 3) {
                    // 똑같은 3개 알파벳이 있는데 다음 입력이 없는경우
                    if (j == str.length() - 1) {
                        msg = "FAKE";
                        break;
                    }
                    // 똑같은 3개 알파벳이 있는데 3개기준으로 앞의 문자가 다른경우
                    else if (str.charAt(j) != str.charAt(j + 1)) {
                        msg = "FAKE";
                        break;
                    }
                    flag = true;
                    alpha[str.charAt(j) - 'A'] = 0; // 3개 검증후 초기화
                }
            }
            System.out.println(msg);
        }
    }
}

0개의 댓글