[SWEA] #1209 Sum

KwonSC·2021년 11월 6일
0

SWEA - Java

목록 보기
4/26
post-thumbnail

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV13_BWKACUCFAYh&categoryId=AV13_BWKACUCFAYh&categoryType=CODE


Code

import java.util.Scanner;

class Solution {
    public static void main(String args[]) throws Exception {
        Scanner sc = new Scanner(System.in);
        for (int testCase = 1; testCase <= 10; testCase++) {
            int z = sc.nextInt();
            int rowMax = 0;
            int col[] = new int[100];
            int diag1 = 0;
            int diag2 = 0;
            for (int i = 0; i < 100; i++) {
                int row = 0;
                for (int j = 0; j < 100; j++) {
                    int temp = sc.nextInt();
                    col[j] += temp;
                    row += temp;
                    if (i == j) {
                        diag1 += temp;
                    }
                    if (i + j == 99) {
                        diag2 += temp;
                    }
                }
                rowMax = Math.max(rowMax, row);
            }
            int colMax = 0;
            for (int i = 0; i < 100; i++) {
                colMax = Math.max(colMax, col[i]);
            }
            System.out.printf("#%d %d\n", testCase, Math.max(rowMax, Math.max(colMax, Math.max(diag1, diag2))));
        }
    }
}

Solution

rowMax : 행의 최대값
colMax : 열의 최대값
diag1 : 남동대각선의 합
diag2 : 남서대각선의 합
배열을 입력받으면서 row와 col[], i == j 일때 남동대각선 diag1, i + j == 99일때 남서대각선 diag2에 현재값 저장
col 배열을 돌면서 colMax값을 찾고 마지막에 4개의 변수중 가장 큰값을 출력

0개의 댓글