• 카테고리

    질문 & 답변
  • 세부 분야

    알고리즘 · 자료구조

  • 해결 여부

    미해결

런타임 에러 질문 있습니다.

23.11.26 21:47 작성 조회수 151

0

 배열 11. 임시반장 정하기 에서 문제를 풀고 있는데해당 코드에서 런타임 에러가 발생하는데 이유를 알 수 있을까요? import java.util.ArrayList; import java.util.Arrays; import java.util.Scanner; public class Main { public static void main(String[] args) { Main t = new Main(); Scanner in = new Scanner(System.in); int n = in.nextInt(); int[][] arr = new int[n][n]; // int[] arr2 = new int[n]; for (int i = 0 ; i < n; i++){ for (int j =0; j < n; j++) arr[i][j] = in.nextInt(); } // for (int i = 0 ; i < n; i++){ // arr2[i] = in.nextInt(); // } int test = t.solution11(n,arr); System.out.print(test); } // 11번 임시반장 private int solution11(int n, int[][] arr){ int answer = 0; int MAX = 0; for (int i =0; i < 5; i ++){ int cnt = 0; for (int j=0; j < n;j++){ int temp = arr[j][i]; for (int k =0; k <n; k++){ if (k == j) continue; else if (temp == arr[k][i]){ cnt++; } } if (cnt > MAX){ MAX = cnt; answer = j; } } } return answer; } } 

답변 2

·

답변을 작성해보세요.

0

안녕하세요^^

학년이 5학년까지로 정해져 있습니다. 입력크기를 5로 하고 학년을 읽는 반복문도 5로 하면 런타임에러는 잡을 수 있습니다. 아래와 같이 하면 에러는 안나옵니다.

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main t = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] arr = new int[n][5];
//        int[] arr2 = new int[n];
        for (int i = 0 ; i < n; i++){
            for (int j =0; j < 5; j++)
                arr[i][j] = in.nextInt();
        }
//        for (int i = 0 ; i < n; i++){
//            arr2[i] = in.nextInt();
//        }
        int test = t.solution11(n,arr);
        System.out.print(test);
    }

    // 11번 임시반장
    private int solution11(int n, int[][] arr){
        int answer = 0;

        int MAX = 0;
        for (int i =0; i < 5; i ++){
            int cnt = 0;
            for (int j=0; j < n;j++){
                int temp = arr[j][i];
                for (int k =0; k <n; k++){
                    if (k == j) continue;
                    else if (temp == arr[k][i]){
                        cnt++;
                    }
                }
                if (cnt > MAX){
                    MAX = cnt;
                    answer = j;
                }
            }
        }
        return answer;
    }

}

0

안녕하세요^^

코드블럭 버튼을 이용해서 풀코드를 올려주시면 좋겠습니다. 위에 올린 것은 보기가 불편합니다.

lleejk96님의 프로필

lleejk96

질문자

2023.11.28

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Main t = new Main();
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int[][] arr = new int[n][n];
//        int[] arr2 = new int[n];
        for (int i = 0 ; i < n; i++){
            for (int j =0; j < n; j++)
                arr[i][j] = in.nextInt();
        }
//        for (int i = 0 ; i < n; i++){
//            arr2[i] = in.nextInt();
//        }
        int test = t.solution11(n,arr);
        System.out.print(test);
    }

    // 11번 임시반장
    private int solution11(int n, int[][] arr){
        int answer = 0;

        int MAX = 0;
        for (int i =0; i < 5; i ++){
            int cnt = 0;
            for (int j=0; j < n;j++){
                int temp = arr[j][i];
                for (int k =0; k <n; k++){
                    if (k == j) continue;
                    else if (temp == arr[k][i]){
                        cnt++;
                    }
                }
                if (cnt > MAX){
                    MAX = cnt;
                    answer = j;
                }
            }
        }
        return answer;
    }

}

아 죄송합니다 ; 다시 올려드렸어요!