[SWEA][D3]1209. [S/W 문제해결 기본] 2일차 - Sum /파이썬/자바
문제출처 |
다음 100X100의 2차원 배열이 주어질 때, 각 행의 합, 각 열의 합, 각 대각선의 합 중 최댓값을 구하는 프로그램을 작성하여라.
다음과 같은 5X5 배열에서 최댓값은 29이다.
[제약 사항]
총 10개의 테스트 케이스가 주어진다.
배열의 크기는 100X100으로 동일하다.
각 행의 합은 integer 범위를 넘어가지 않는다.
동일한 최댓값이 있을 경우, 하나의 값만 출력한다.
[입력]
각 테스트 케이스의 첫 줄에는 테스트 케이스 번호가 주어지고 그 다음 줄부터는 2차원 배열의 각 행 값이 주어진다.
[출력]
#부호와 함께 테스트 케이스의 번호를 출력하고, 공백 문자 후 테스트 케이스의 답을 출력한다.
1 python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#20.02.03 # for T in range(1,11):
t=int(input()) mylist=[list(map(int,input().split()))for _ in range(100)] sumlist,c,d=[],[],[] for i in range(100): sumlist.append(sum(mylist[i])) b=[] for j in range(100): b.append(mylist[j][i]) if i==j: c.append(mylist[i][j]) elif i+j==2: d.append(mylist[i][j]) sumlist.append(sum(b)) sumlist.append(sum(c)) sumlist.append(sum(d)) print('#{} {}'.format(t,max(sumlist)))
|
2 JAVA (1) 직관적
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 |
import java.util.Arrays; import java.util.Scanner;
//21.01.01 //1209. [S/W 문제해결 기본] 2일차 - Sum D3 class Solution { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); for (int t = 0; t < 10; t++) { // 입력 int result = 0; int tc = sc.nextInt(); int[][] arr = new int[100][100]; for (int y = 0; y < 100; y++) { for (int x = 0; x < 100; x++) { arr[x][y] = sc.nextInt(); } } int garoSum; int seroSum=0; int ltor=0; int rtol=0; // 문제풀이 for (int y = 0; y < 100; y++) { //대각선 ltor += arr[y][y]; rtol += arr[y][99-y]; //가로 행 garoSum = Arrays.stream(arr[y]).sum(); if (result < garoSum) { result = garoSum; } //세로 열 for (int x = 0; x < 100; x++) { seroSum += arr[x][y]; } if (result < seroSum) { result = seroSum; } seroSum =0; } if (result < ltor) { result = ltor; } if (result < rtol) { result = rtol; } System.out.println("#"+tc+" "+result); }
} }
|
3 JAVA (2) Stream
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 |
import java.util.Arrays; import java.util.Scanner;
//21.01.01 //1209. [S/W 문제해결 기본] 2일차 - Sum D3 class Solution { public static void main(String[] args) throws Exception { Scanner sc = new Scanner(System.in); for (int t = 0; t < 10; t++) { // 입력 int result = 0; int tc = sc.nextInt(); int[][] arr = new int[100][100]; for (int y = 0; y < 100; y++) { for (int x = 0; x < 100; x++) { arr[x][y] = sc.nextInt(); } } int garoSum; int seroSum=0; int ltor=0; int rtol=0; // 문제풀이 Stream이용 int max = -1; for (int y = 0; y < 100; y++) { int finalY = y; int sum1 = IntStream.range(0, 100).map(it -> arr[finalY][it]).sum(); int sum2 = IntStream.range(0, 100).map(it -> arr[it][finalY]).sum();
max = Math.max(Math.max(sum1, sum2), max); } int cross1 = IntStream.range(0, 100).map(it -> arr[it][it]).sum(); int cross2 = IntStream.range(0, 100).map(it -> arr[99-it][99-it]).sum();
max = Math.max(max, Math.max(cross1, cross2)); System.out.println("#"+tc+" "+max); }
} }
|