• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

왜 오답인지 정말 모르겠습니다...

23.02.24 14:23 작성 23.02.24 14:34 수정 조회수 344

0

임시반장 정하기 문제에서

import java.util.Scanner;

public class Main {

    private static void solution (int num, int[][] arr){
        int[][] test_arr = new int[num][5];
        int[] result = new int[num];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < num-1; j++) {
                for (int k = j+1; k < num; k++) {
                    if (arr[j][i] == arr[k][i]){
                        test_arr[j][i] = 1;
                        test_arr[k][i] = 1;
                    }
                }
            }
        }
        int result_index =0;
        int max = 0;
        for (int i = 0; i < num; i++) {
            for (int j = 0; j < num; j++) {
                if(test_arr[i][j] != 0){
                    result[i] += test_arr[i][j];
                }
            }
            if (result[i] > max){
                max = result[i];
                result_index = i+1;
            }
        }
        System.out.println(result_index);
    }
    public static void main(String[] args) {
        Main T = new Main();
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(sc.nextLine());
        int[][] arr = new int[num][5];
        for (int i = 0; i < num; i++) {
            String[] temp = sc.nextLine().split(" ");
            for (int j = 0; j < 5; j++) {
                arr[i][j] = Integer.parseInt(temp[j]);
            }

        }
        T.solution(num, arr);
    }
}

이런식으로 3개의 for문을 써서 해결했는데요 테스트케이스들은 다 통과하는데

이라고 뜨네요 왜인지 이유를 모르겠습니다...

답변 1

답변을 작성해보세요.

0

안녕하세요^^

아래 입력은 답이 3입니다. 위에 코드는 4가 나옵니다. 스스로 디버그해보세요.

5
9 8 7 6 5
5 6 7 8 9
1 2 3 7 8
4 5 3 4 2
6 2 8 4 2