728x90

https://swexpertacademy.com/main/code/problem/problemDetail.do?contestProbId=AV5PzOCKAigDFAUq 

 

SW Expert Academy

SW 프로그래밍 역량 강화에 도움이 되는 다양한 학습 컨텐츠를 확인하세요!

swexpertacademy.com

 

 

문제)

 

 

 

풀이)

아주 간단한 문제!

N크기도 작아서 for문으로 충분히 해결할 수 있다고 생각했다.

 

네모칸을 차례대로 움직이면서, 값을 모두 더한 후 비교하는 방식을 이용했는데,

범위만 주의하면 간단하다.

 

 

Python 코드

T = int(input())
for test_case in range(1, T + 1):
    n, m = map(int, input().split())
    matrix = []
    for _ in range(n):
        matrix.append(list(map(int, input().split())))

    answer = 0
    for i in range(n-m+1):
        for j in range(n-m+1):
            num = 0
            for row in range(m):
                for col in range(m):
                    num += matrix[row+i][col+j]
            if answer < num:
                answer = num
			           

    print('#{} {}'.format(test_case, answer))

 

 

JAVA 코드

import java.util.Scanner;
import java.io.FileInputStream;

class Solution
{
	public static void main(String args[]) throws Exception
	{
		Scanner sc = new Scanner(System.in);
		int T;
		T=sc.nextInt();
		for(int test_case = 1; test_case <= T; test_case++)
		{
		int N = sc.nextInt();
			int M = sc.nextInt();
			int[][] matrix = new int[N][N];
			for(int i=0; i<N; i++) {
				for(int j=0; j<N; j++) {
					matrix[i][j] = sc.nextInt();
				}
			}
			
			int answer = 0;
			for(int row=0; row<N-M+1; row++) {
				for(int col=0; col<N-M+1; col++) {
					int num=0;
					for(int i=0; i<M; i++) {
						for(int j=0; j<M; j++) {
							num += matrix[row+i][col+j];
						}
					}
					if(answer<num)
						answer=num;
				}
			}
			System.out.println("#"+test_case+" "+answer);

		}
	}
}
728x90

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

SWEA 1228 암호문1(JAVA)  (0) 2022.08.19
SWEA 1225. 암호생성기(JAVA)  (0) 2022.08.16
SWEA 1218. 괄호 짝짓기(JAVA)  (0) 2022.08.12
SWEA 2805. 농작물 수확하기(JAVA)  (0) 2022.08.12
SWEA 1873. 상호의 배틀필드(JAVA)  (0) 2022.08.10

+ Recent posts