• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

런타임 에러

21.07.20 00:37 작성 조회수 103

0

테스트 케이스에서는 오류가 안나는데 채점을 하면 런타임 에러가 나오네요...

강사님과 코드를 좀 다른방식으로 작성해서 비교가 불가능합니다. 코드 한 번만 봐주시면 감사하겠습니다.

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        int n = sc.nextInt();

        int[][] arr = new int[n][5];
        int[] same = new int[n];
        boolean[] check = new boolean[n];

        for(int i = 0; i < n; i++) {
            for(int j = 0; j < 5; j++) {
                arr[i][j] = sc.nextInt();
            }
        }
        int cnt = 0;
        for(int i = 0; i < n; i++) {
            for(int j = 0; j < 5; j++) {
                for(int k = 0; k < n; k++) {
                    if(arr[i][j] == arr[j][k]) check[k] = true;
                }
            }
            for(int l = 0; l < n; l++) {
                if(check[l] == true) cnt++;
                check[l] = false;
            }
            same[i] = cnt;
            cnt = 0;
        }

        int max = same[0];
        int tmp = 0;
        for(int i = 1; i < n; i++) {
            if(max < same[i]) {
                max = same[i];
                tmp = i;
            } else if(max == same[i])
                tmp = tmp < i ? tmp : i;
        }
        System.out.println(tmp);
    }
}

답변 1

답변을 작성해보세요.

0

안녕하세요^^

3중 for문에서 index out of range 에러가 납니다.

학생수는 5명으로 고정되지 않습니다. 아래 입력으로 실행해보세요.

3

5 4 4 2 1

1 2 3 4 5

1 2 3 7 8

답은 2입니다.