끄적끄적 코딩
article thumbnail
728x90

모든 경우의 수를 전부 확인한 후에 높은 값을 출력했습니다.

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 ans = 0;
            int n = sc.nextInt();
            int m = sc.nextInt();
            int temp;
            int sum;
            int mul;
            boolean chk = false;
            if(n > m){
                temp = n;
                n = m;
                m = temp;
                chk = true;
            }
            int[] small = new int[n];
            int[] big = new int[m];
             
            if(!chk){
                for(int i=0; i<small.length; ++i){
                    small[i] = sc.nextInt();   
                }
                for(int i=0; i<big.length; ++i){
                    big[i] = sc.nextInt();   
                }
            } else {
                for(int i=0; i<big.length; ++i){
                    big[i] = sc.nextInt();   
                }
                for(int i=0; i<small.length; ++i){
                    small[i] = sc.nextInt();   
                }
            }
             
            for(int i=0; i<m-n+1; ++i){
                sum = 0;
                for(int j=0; j<n; ++j){
                    mul = small[j] * big[i+j];
                    sum += mul;      
                }                
                ans = Math.max(sum, ans);
            }
            System.out.printf("#%d %d%n", test_case, ans);
        }
    }
}

 

'알고리즘' 카테고리의 다른 글

[Java] SWEA - 중간값 찾기  (0) 2023.01.12
[Java] SWEA - 최대수 구하기  (0) 2023.01.12
[Java] SWEA - Sum  (0) 2023.01.12
[Java] SWEA - 최빈수 구하기  (0) 2023.01.12
[Java] SWEA - 평균값 구하기  (0) 2023.01.12

검색 태그