개발놀이터

2-11. 임시반장 본문

기타/코딩테스트

2-11. 임시반장

마늘냄새폴폴 2023. 2. 14. 12:46

 

package 배열1차원2차원.임시반장2다시11.my;

import java.util.Scanner;

public class Main {

    /**
     * 문제가 너무 이상해서 이런식으로는 절대 코딩테스트 안나올듯
     * 오해의 소지가 너무 분명함
     */

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int input = kb.nextInt();
        int[][] arr = new int[input][5];

        for (int i = 0; i < input; i++) {
            for (int j = 0; j < 5; j++) {
                arr[i][j] = kb.nextInt();
            }
        }

        System.out.println(solution(input, arr));
    }

    private static int solution(int input, int[][] arr) {
        int answer = 0, cnt = 0, student = 0, num = 0, max = 0;
        int[] count = new int[input];

        for (int k = 0; k < input; k++) {
            for (int i = 0; i < 5; i++) {
                for (int j = 0; j < input; j++) {
                    if (arr[student][num] == arr[j][i]) {
                        cnt++;
                    }
                }
                num++;
            }
            student++;
            count[k] = cnt;
            cnt = 0;
            num = 0;
            if (max < count[k]) {
                max = count[k];
            }
        }

        for (int i = 0; i < input; i++) {
            if (count[i] == max) {
                answer = i + 1;
                break;
            }
        }

        return answer;
    }
}

 

package 배열1차원2차원.임시반장2다시11.teacher;

import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        int[][] arr = new int[n + 1][6];
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= 5; j++) {
                arr[i][j] = kb.nextInt();
            }
        }
        System.out.println(solution(n, arr));
    }

    private static int solution(int n, int[][] arr) {
        int answer = 0, max = Integer.MIN_VALUE;

        for (int i = 1; i <= n; i++) {
            int cnt = 0;
            for (int j = 1; j <= n; j++) {
                for (int k = 1; k <= 5; k++) {
                    if (arr[i][k] == arr[j][k]) {
                        cnt++;
                        break;
                    }
                }
            }
            if (max < cnt) {
                max = cnt;
                answer = i;
            }
        }

        return answer;
    }
}

 

문제가 너무 오해의 소지가 많아 실제 코딩테스트로는 안나올듯한 문제 그냥 넘어가자

'기타 > 코딩테스트' 카테고리의 다른 글

2장 배열 주요개념 및 복습 노트  (0) 2023.02.14
2-12. 멘토링  (0) 2023.02.14
2-10. 봉우리  (0) 2023.02.14
2-9 격자판 최대합  (0) 2023.02.14
2-8. 등수 구하기  (0) 2023.02.14