두 개의 숫자열 | SWEA

Bluewave·2024년 7월 1일

코테공부_java

목록 보기
40/99
post-thumbnail

문제

✏️ 문제 바로가기

문제레벨정답률
두 개의 숫자열D283%

My Code

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();
        int[] max_ = new int[T];
 
        for(int test_case = 1; test_case <= T; test_case++)
        {
            int N = sc.nextInt();
            int M = sc.nextInt();
            sc.nextLine();
            String a = sc.nextLine();
            String b = sc.nextLine();
            String[] A = a.split(" ");
            String[] B = b.split(" ");
            int max = 0;
             
            int cnt = 0; int smallCnt = 0;
            if(N>M) {cnt = N-M; smallCnt = M;}
            else {cnt = M-N;smallCnt = N;}
             
            for(int i = 0; i<=cnt; i++) {
                int current = 0; 
                for(int j = 0; j<smallCnt; j++) {
                    if(smallCnt == N) {
                        current += Integer.parseInt(B[i+j]) * Integer.parseInt(A[j]);
                    } else {
                        current += Integer.parseInt(B[j]) * Integer.parseInt(A[i+j]);
                    }
                }
                if(max<current) {
                    max = current;
                }
            }
     
            max_[test_case-1] = max;
        }
         
        for(int test_case = 1; test_case<=T; test_case++) {
            System.out.println("#" + test_case + " " + max_[test_case-1]);
        }
         
        sc.close();
    }
}

String to int

Integer.parseInt()

int to String

Integer.toString()

+) nextInt 다음에 nextLine이 올때는 중간에 nextLine을 한 번 넣어주기!!
ㄴ 그래야 다음 열을 읽어들일 수 있음


최적화 코드

import java.util.Scanner;

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

        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 maxSum = Integer.MIN_VALUE;

            if (N > M) {
                for (int i = 0; i <= N - M; i++) {
                    int currentSum = 0;
                    for (int j = 0; j < M; j++) {
                        currentSum += A[i + j] * B[j];
                    }
                    if (currentSum > maxSum) {
                        maxSum = currentSum;
                    }
                }
            } else {
                for (int i = 0; i <= M - N; i++) {
                    int currentSum = 0;
                    for (int j = 0; j < N; j++) {
                        currentSum += B[i + j] * A[j];
                    }
                    if (currentSum > maxSum) {
                        maxSum = currentSum;
                    }
                }
            }

            maxResults[test_case - 1] = maxSum;
        }

        for (int test_case = 1; test_case <= T; test_case++) {
            System.out.println("#" + test_case + " " + maxResults[test_case - 1]);
        }

        sc.close();
    }
}

가장 큰 차이점은 입력값을 받아올때 방식이다.
nextLine과 문자열 분할 대신 nextInt를 사용하여 정수 배열을 직접 읽어왔다.

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

여러 정수를 한 줄에서 읽어와야한다면 for문을 사용해서 배열에 저장하자!

profile
Developer's Logbook

0개의 댓글