본문 바로가기

알고리즘/문제 풀이

SW Expert Academy 1959 두 개의 숫자열

SW Expert Academy

 

SW Expert Academy

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

swexpertacademy.com

import java.util.*;


class Solution
{
    public static void main(String args[])
    {
        Scanner sc = new Scanner(System.in);
        int T = sc.nextInt();


        for (int test_case = 1; test_case <= T; test_case++) {
            ArrayList<Integer> list = new ArrayList<>();
            int a_size = sc.nextInt();
            int b_size = sc.nextInt();
            int [] A = new int[a_size];
            int [] B = new int[b_size];

            for (int i = 0; i < a_size; i++) {
                A[i] = sc.nextInt();
            }
            for (int i = 0; i < b_size; i++) {
                B[i] = sc.nextInt();
            }
            

            // 두 배열 중 뭐가 더 긴 지 확인한다.
            if(A.length> B.length){
                multiply(list, B,A);
            }else {
                multiply(list,A,B);
            }

            // list 중 제일 큰 값 출력 
            System.out.println("#"+test_case+" "+(list.stream().max(new Comparator<Integer>() {
                @Override
                public int compare(Integer o1, Integer o2) {
                    return o1 -o2;
                }
            }).orElse(0)));
        }
    }
    
    static void multiply (ArrayList<Integer> list, int small[], int big[]) { // 작은 배열을 이동시켜가며 계산 temp에 결과 저장
        for (int j = 0; j <= big.length- small.length; j++)
        {
            int temp = 0;
            for (int i = 0; i < small.length; i++) {
                temp += small[i] * big[i+j];
            }
            list.add(temp);
            temp = 0;
        }
    }
}

'알고리즘 > 문제 풀이' 카테고리의 다른 글

SW Expert Academy 1948. D2 날짜 계산기  (0) 2023.05.16
SW Expert Academy 1954 달팽이 숫자  (0) 2023.05.15
1961 회전 숫자 배열  (0) 2023.05.15
1996. 숫자를 정렬하자  (0) 2023.05.15
SW D2 1970 쉬운 거스름돈  (0) 2023.05.15