列表

详情


NC214665. Maximumsubsegmentproduct

描述

Give you n real numbers(实数), each of which is greater than 0 and less than 2, find the largest product (乘积)of the continuous subsegment(这n个数中连续的一段)

输入描述

input n(n<=10^5) in the first line
Next n lines, input a number for each line

输出描述

Output a number with two decimal places(保留两位小数)

示例1

输入:

10
1.24237
1.13899
0.7366
1.21042
1.28331
0.84378
1.27088
1.21075
0.73157
1.7932

输出:

2.76

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

Python3 解法, 执行用时: 922ms, 内存消耗: 12596K, 提交时间: 2023-04-09 08:43:59

x=int(input())
ls=[]
for i in range(x):
    num=eval(input())
    if 0<=num<2:
        ls.append(num)
dp=[0]*x
len1=len(ls)
dp[0]=ls[0]
for i in range(1,len1):
    dp[i]=max(ls[i],dp[i-1]*ls[i])
print('{:.2f}'.format(max(dp)))

C++(clang++11) 解法, 执行用时: 59ms, 内存消耗: 484K, 提交时间: 2020-12-27 21:54:36

#include<bits/stdc++.h>
using namespace std;
int main(){
	int n;
	cin >> n;
	double maxx=0;
	double ans=1;
	double t;
	while(n--){
		cin >> t;
		ans*=t;
		maxx=max(maxx,ans);
		if(ans<1)
		ans=1;
	}
	printf("%.2f",maxx);
}

pypy3 解法, 执行用时: 287ms, 内存消耗: 29924K, 提交时间: 2023-04-09 13:47:10

N = int(input())
lis = [float(input()) for _ in range(N)]
data = [0.00] * N
data[0] = lis[0]
for i in range(1, N):
    data[i] = max(lis[i], lis[i] * data[i - 1])
print(f"{max(data):.2f}")

上一题