SWEA

[SWEA] 두 개의 숫자열

itsnot4me 2024. 10. 4. 16:03

https://swexpertacademy.com/main/code/problem/problemDetail.do?problemLevel=2&contestProbId=AV5PpoFaAS4DFAUq&categoryId=AV5PpoFaAS4DFAUq&categoryType=CODE&problemTitle=&orderBy=FIRST_REG_DATETIME&selectCodeLang=ALL&select-1=2&pageSize=10&pageIndex=2

 

SW Expert Academy

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

swexpertacademy.com

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[] array1 = new int[N];
            int[] array2 = new int[M];
            for(int i=0; i<N; i++){
                array1[i]=sc.nextInt();
            }
            for(int j=0; j<M; j++){
                array2[j]=sc.nextInt();
            }
            int sum=0;
            int max=-1231434234;
            if(N>M){
                for(int k=0; k<N-M; k++){
                    for(int l=0; l<M; l++){
						sum+=array1[l+k]*array2[l];
                    }
                    if(max<sum)
                    max=sum;
                    sum=0;
                }
            }
                if(M>N){
                for(int k=0; k<=M-N; k++){
                    for(int l=0; l<N; l++){
                            sum+=array1[l]*array2[l+k];
                    }
                    if(max<sum)
                        max=sum;
                    sum=0;
                }
                   
                }
			System.out.println("#"+test_case+" "+max);
            
		}
    }
}

 

저장값을 배열에 넣고 최댓값을 출력한다는 식으로 했으면 아마 훨씬 깔끔했을 것이다

문제 자체는 쉬우니 for문으로만 풀었다면 그러는게 좋을 듯