백준 1268 java : 구현

magicdrill·2025년 6월 2일
0

백준 문제풀이

목록 보기
613/655

백준 1268 java : 구현

왜 틀렸는가?

테스트 케이스도 전부 통과를 했는데 계속 틀렸다고 나왔다. 최댓값을 0으로 한게 문제였다.

public static int findAnswer() {
    int answer = 0;
    int maxCount = 0;
    int i, j, k;
    int count;
        ........
}

처음에 maxCount를 0으로 초기화하고 시작했는데, 이때 서로 아무도 만난 적이 없다면 가장 먼저 번호인 1번 학생이 나와야 하는데 그렇지 않았다.

public static int findAnswer() {
    int answer = 0;
    int maxCount = Integer.MIN_VALUE;
    int i, j, k;
    int count;
}

최댓값을 정수형 최솟값, 하다 못해 -1로라도 했으면 오래 걸릴 문제가 아니었다.

import java.util.Scanner;

public class bj1286 {
    static Scanner sc = new Scanner(System.in);
    static int N;
    static int [][] studentData;

    public static void main(String[] args) {
        inputData();
        System.out.println(findAnswer());

        sc.close();
    }

    public static void inputData() {
        int i, j;

        N = sc.nextInt();
        studentData = new int[N][5];
        for(i = 0; i < N; i++) {
            for(j = 0; j < 5; j++) {
                studentData[i][j] = sc.nextInt();
            }
        }
    }

    public static int findAnswer() {
        int answer = 0;
        int maxCount = Integer.MIN_VALUE;
        int i, j, k;
        int count;

        for (i = 0; i < N; i++) {
            System.out.print(i + 1 + "번 학생 : ");
            count = 0;

            for (j = 0; j < N; j++) {
                if (i == j) {
                    continue;
                }
                for (k = 0; k < 5; k++) {
                    if (studentData[i][k] == studentData[j][k]) {
                        count++;
                        break;
                    }
                }
            }
            System.out.println(count);

            if (count > maxCount) {
                maxCount = count;
                answer = i + 1; // 1번부터 시작
            }
        }

        return answer;
    }
}

0개의 댓글