어려운 문제인줄 알았는데 쉬운 문제였다.

우선 완전탐색부터 해봐야한다는 것을 알았다.

#include <iostream>
#include <stdio.h>
#include <cmath>
using namespace std;
double arr[10001];
int n;
double maxvalue = -1;
void process(){
    for (int i = 0; i < n; i++){
        double sum = 1;
        for (int j = i; j < n; j++){
            sum *= arr[j];
            if (sum > maxvalue) maxvalue = sum;
        }
    }
}
int main(){
    cin >> n;
    for (int i = 0; i < n; i++){
        cin >> arr[i];
    }
    process();
    cout.setf(ios::fixed);
    cout.precision(3);
    cout << maxvalue << endl;
    return 0;
}
 
cs


DP로 푼 방법(참고)

#pragma warning(disable : 4996)
#include<stdio.h>
#include<iostream>
using namespace std;
 
double best,basic;
double data[10005];
int num,i,j;
 
int main(){
    cin >> num;
    for(i=0; i<num; i++){
        scanf("%lf",&data[i]);
    }
 
    best=basic=data[0];
 
    for(j=1; j<num; j++){
        if(data[j]>basic*data[j]){
            basic = data[j];
        }
        else{basic*=data[j];}
        if(basic > best){best = basic;}
    }
 
    printf("%.3lf",best);
    return 0;
}
 
cs



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

더블릿_짧은노래  (0) 2015.04.14
면접알고리즘시그_1주차  (0) 2015.04.09
2668_숫자고르기  (0) 2015.04.09
더블릿_더큰  (0) 2015.04.08
더블릿_계단오르기(dp)  (2) 2015.04.07
Posted by slender ankles
,