java
N의 길이가 길 때와 M의 길이가 길 때를 따로 분리해주고, 2중 for 문을 돌면서 변수 result 에 곱한 숫자의 합을 넣고 그 중 최대값을 구했다.
각 배열의 인덱스 위치가 헷갈릴 수 있는 문제입니다. 직접 그려보면서 하면 좀 더 빠르게 이해할 수 있습니다.
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[] A = new int[N];
int[] B = new int[M];
for(int i = 0; i < N; i++) A[i] = sc.nextInt();
for(int i = 0; i < M; i++) B[i] = sc.nextInt();
int max = 0;
if(N < M) {
for(int i = 0; i < M-N+1; i++) {
int result = 0;
for(int j = 0; j < N; j++) {
result += A[j]*B[i+j];
}
max = Math.max(max, result);
}
} else if (N > M) {
for(int i = 0; i < N-M+1; i++) {
int result = 0;
for(int j = 0; j < M; j++) {
result += A[i+j]*B[j];
}
max = Math.max(max, result);
}
} else {
int result = 0;
for(int i = 0; i < N; i++) {
result += A[i]*B[i];
}
max = Math.max(max, result);
}
System.out.printf("#%d %d\n", test_case, max);
}
}
}
Java들어가며 소프트웨어를 개발할 때 메모리 관리 방식은 프로그램의 안정성과 성능을 좌우하는 핵심 요소입니다. 특히 자바스크립트,…
들어가며 소프트웨어 개발자는 코드가 어떻게 실행되는지 정확히 이해해야 할 필요가 있습니다. 우리가 작성한 프로그램은 결국…
서론 현대 웹 애플리케이션 아키텍처에서 웹 서버(Web Server) 와 웹 애플리케이션 서버(WAS, Web Application Server)…
HTTP 헤더(Header)란? HTTP(Header)는 클라이언트와 서버 간에 교환되는 메타데이터로, 요청(Request)과 응답(Response)에 부가적인 정보를 실어 나르는 역할을…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…
Readable Code: 읽기 좋은 코드를 작성하는 사고법Practical Testing: 실용적인 테스트 가이드 강의와 함께한 인프런 워밍업 클럽…