코딩테스트

[백준-2670번] 연속부분최대곱 풀이 - Java

UnaUna 2025. 6. 6. 01:20
반응형

🚀 문제

https://www.acmicpc.net/problem/2670

🚀 접근 방법

백준 1912번 풀이와 비슷하다.

구간 별 최대 값들을 저장하고, 가장 큰 값을 출력하면 된다.

주의할 점은 data저장과 dp 저장 자료형을 double로 선언할 것

 

🚀 코드

import java.io.*;
import java.util.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));

        int n = Integer.parseInt(in.readLine());

        double[] data = new double[n];
        double[] dp = new double[n];

        dp[0] = data[0];

        for(int i=1; i<n; i++){
            data[i] = Double.parseDouble(in.readLine());
            dp[i] = Math.max(data[i], dp[i-1]*data[i]);
        }

        double max = 0;
        for(int i=0; i<n; i++){
            if(max < dp[i]){
                max = dp[i];
            }
        }

        System.out.printf("%.3f",max);
    }
}
반응형