두 개의 숫자열


Software Expert Academy [1959] 두 개의 숫자열

문제보기
Alt text

소스코드

import java.util.Scanner;
 
public class Solution {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();
 
        for (int tc = 1; tc <= T; tc++) {
            int N = sc.nextInt();
            int M = sc.nextInt();
            int[] A = new int[N];
            int[] B = new int[M];
 
            for (int i = 0; i < N; i++) {
                A[i] = sc.nextInt();
            }
            for (int j = 0; j < M; j++) {
                B[j] = sc.nextInt();
            }
 
            System.out.print("#" + tc + " ");
            if (N <= M) {
                System.out.println(mulFunc(A, B));
            } else {
                System.out.println(mulFunc(B, A));
            }
        }
    }
 
    public static int mulFunc(int[] small, int[] big) {
        int limit = big.length - small.length;
        int ans = -987654321;
        for (int i = 0; i <= limit; i++) {
            int chk = 0;
            for (int j = 0; j < small.length; j++) {
                chk += small[j] * big[i + j];
            }
            ans = Math.max(ans, chk);
        }
 
        return ans;
    }
}