profile image

L o a d i n g . . .


1
import java.util.Scanner;

public class solution_d2_1959 {

public static void main(String[] args) {
// 1959. 두개의 숫자열
Scanner sc = new Scanner(System.in);

// 반복 횟수
int chaces = sc.nextInt();

// 반복 횟수만큼 반복
for (int i = 0; i < chaces; i++) {
int max=0;
int total;

// 길이 입력
int N = sc.nextInt();
int M = sc.nextInt();

// 입력한 길이로 배열 생성
int[] A = new int[N];
int[] B = new int[M];

// 각 배열에 원소 직접입력
for (int j = 0; j < N; j++) {
A[j] = sc.nextInt();
}
for (int k = 0; k < M; k++) {
B[k] = sc.nextInt();
}

int len=Math.abs(N-M);

//A배열이 B배열보다 큰 경우
if (A.length > B.length) {
for (int x = 0; x < len + 1; x++) {
total=0;
for (int z=0; z<B.length; z++) {
total+=A[z+x]*B[z];
}
if (max<total) max=total;
}
}

//B배열이 A배열 보드 큰 경우
else if (B.length > A.length) {
for (int x = 0; x < len + 1; x++) {
total=0;
for (int z=0; z<A.length; z++) {
total+=A[z]*B[z+x];
}
if (max<total) max=total;
}
}

//길이가 같은 경우
else {
total=0;
for (int z=0; z<N; z++) {
total+=A[z]*B[z];
}
max=total;
}

System.out.println("#"+(i+1)+" "+max);

}

}
}
cs

 

 

복사했습니다!