Hyunebee

2차원 배열의 합 본문

Java/코테

2차원 배열의 합

Hyunebee 2022. 3. 5. 21:11
import java.util.*;
class Main {
    public int solution(int[][] arr, int n){

        int MAX_Result = 0;
        int result_w = 0;
        int result_h = 0;

        for (int i = 0; i < n; i++){ 
            for (int j =0; j < n; j++){
                result_w += arr[i][j]; // 가로방향의 합
                result_h += arr[j][i]; // 세로방향의 합
            }

            MAX_Result = Math.max(result_h,MAX_Result);
            MAX_Result = Math.max(result_w,MAX_Result);
            result_w = result_h = 0;
        }

        result_w = 0;
        result_h = 0;

        for(int i =0; i < n; i++){
            result_w += arr[i][i];// 우측 아래 대각선
            result_h += arr[i][n-i-1];// 좌측 아래 대각선
        }

        MAX_Result = Math.max(result_h, MAX_Result);
        MAX_Result = Math.max(result_w, MAX_Result);

        return MAX_Result;
    }
    public static void main(String[] args){
        Main T = new Main();
        Scanner kb = new Scanner(System.in);
        int n = kb.nextInt();
        int[][] arr=new int[n][n];
        for(int i=0; i<n; i++){
            for(int j = 0; j < n; j++){
                arr[i][j] = kb.nextInt();
            }
        }

        System.out.println(T.solution(arr, n));

    }
}

 

'Java > 코테' 카테고리의 다른 글

임시반장정하기  (0) 2022.03.07
봉우리 찾기  (0) 2022.03.06
등수구하기 Rank  (0) 2022.03.03
점수계산하기  (0) 2022.03.03
에라토스테네스 체  (0) 2022.02.27