ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [SWEA] S/W 문제해결 기본(2) - Sum
    Algorithm/SWEA 2020. 4. 16. 17:51

    [1209] Sum

    • 다음 100X100의 2차원 배열이 주어질 때, 각 행의 합, 각 열의 합, 각 대각선의 합 중 최댓값을 구하는 프로그램을 작성하여라.

     

     

    Solution

    • 완전 탐색으로 모든 합 비교 

     

     

    소스코드

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.StringTokenizer;
     
    public class Solution {
        static int T, n, m, distance, answer;
        static int[][] map;
        static StringTokenizer st;
        static String line;
     
        public static void main(String[] args) throws IOException {
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
     
            while (true) {
                T = stoi(br.readLine());
                init();
                answer = Integer.MIN_VALUE;
                
                // Row
                int sum;
                for (int i = 0; i < 100; i++) {
                    sum = 0;
                    st = new StringTokenizer(br.readLine());
                    for (int j = 0; j < 100; j++) {
                        map[i][j] = stoi(st.nextToken());
                        sum += map[i][j];
                    }                
                    answer = Math.max(answer, sum);
                }
     
                // Col
                for(int i = 0; i<100; i++) {
                    sum = 0;
                    for(int j = 0; j<100; j++) {
                        sum += map[j][i];
                    }
                    answer = Math.max(answer, sum);
                }
                
                sum = 0;
                for(int i = 0; i<100; i++) {
                    sum += map[i][i];
                }
                answer = Math.max(answer, sum);
                
                
                sum = 0;
                for(int i = 0; i<100; i++) {
                    sum += map[i][99-i];
                }
                answer = Math.max(answer, sum);
                
                System.out.println("#" + T + " " + answer);
     
                if (T == 10)
                    break;
            }
     
        }
     
        static void init() {
            map = new int[100][100];
        }
     
        static int stoi(String s) {
            return Integer.parseInt(s);
        }
    }
    cs

    댓글

Designed by Tistory.