最大连续子序列乘积_1501

题目描述:

给定一个浮点数序列(可能有正数、0和负数),求出一个最大的连续子序列乘积。

输入:

输入可能包含多个测试样例。
每个测试样例的第一行仅包含正整数 n(n<=100000),表示浮点数序列的个数。
第二行输入n个浮点数用空格分隔。
输入数据保证所有数字乘积在双精度浮点数表示的范围内。

输出:

对应每个测试案例,输出序列中最大的连续子序列乘积,若乘积为浮点数请保留2位小数,如果最大乘积为负数,输出-1。

样例输入:
7
-2.5 4 0 3 0.5 8 -1
5
-3.2 5 -1.6 1 2.5
5
-1.1 2.2 -1.1 3.3 -1.1
样例输出:
12
64
8.78
 
  
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.StreamTokenizer;
 
public class zuichangzixulie1501 {
    public static void main(String[] args) throws Exception {
        StreamTokenizer st = new StreamTokenizer(new BufferedReader(new InputStreamReader(System.in)));
        while (st.nextToken() != StreamTokenizer.TT_EOF) {
            int n = (int) st.nval;
            st.nextToken();
            double firstNum =  st.nval;
            double res = firstNum;
            double min = firstNum;
            double max = firstNum;
            double temp1 = 0;
            double temp2 = 0;
            for (int i = 2; i < n+1; i++) {
                st.nextToken();
                double num =  st.nval;
                temp1 = min * num;
                temp2 = max * num;
                max = Math.max(Math.max(temp1, temp2), num);
                min = Math.min(Math.min(temp1, temp2), num);
                if (max > res) {
                    res = max;
                }
            }
            int secRes = (int)res;
            if (res < 0) {
                System.out.println(-1);
            }else if (res - secRes == 0) {
                System.out.println(secRes);
            }else {
                System.out.printf("%.2f\n", res);
            }
        }
    }
}
  
</pre><pre name="code" class="java">也可以用动态规划 状态转移方程
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">状态<span style="color: rgb(255, 0, 0);"><strong>Max[i]</strong></span>表示以data[i]结尾的最大连续子串乘积值,<span style="color: rgb(255, 0, 0);"><strong>Min[i]</strong></span>表示以data[i]结尾的最小连续子串乘积值(考虑到负数的情况)。</p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;">状态转移方程:<span style="color: rgb(255, 0, 0);"><strong>Max[i] = max(data[i], Max[i-1]*data[i], Min[i-1]*data[i]),</strong></span></p><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; color: rgb(85, 85, 85); font-family: 'microsoft yahei'; font-size: 15px; line-height: 35px;"><span style="color: rgb(255, 0, 0);"><strong>                         Min[i] =  min(data[i], Max[i-1]*data[i],  Min[i-1]*data[i]).</strong></span></p><pre name="code" class="cpp">#include <stdio.h>  
   
#define LEN 100000  
   
int N;  
double data[LEN];  
double max[LEN];  
double min[LEN];  
   
double Max(double a, double b){  
    return (a > b) ? a : b;  
}  
   
double Min(double a, double b){  
    return (a < b) ? a : b;  
}  
   
double MMS(){  
    int i;  
    double ans;  
    max[0] = min[0] = data[0];  
    ans = data[0];  
    for (i = 1; i < N; ++i){  
        max[i] = Max(Max(data[i], max[i-1]*data[i]), min[i-1]*data[i]);  
        min[i] = Min(Min(data[i], max[i-1]*data[i]), min[i-1]*data[i]);  
        ans = Max(ans, max[i]);  
    }  
    return ans;  
}  
   
int main(void){  
    int i;  
    double ans;  
    while (scanf("%d", &N) != EOF){  
        for (i = 0; i < N; ++i)  
            scanf("%lf", &data[i]);  
        ans = MMS();  
        if (ans < 0)  
            printf("-1\n");  
        else if (ans - (int)ans <= 1e-5)  
            printf("%lld\n", (int)ans);  
        else  
            printf("%.2lf\n", ans);  
    }  
   
    return 0;  
} 


</pre><pre name="code" class="java">
参考文章:http://blog.csdn.net/v_july_v/article/details/8701148
参考文章:http://blog.csdn.net/sunnyyoona/article/details/43834343
参考文章:http://blog.csdn.net/wzy_1988/article/details/9319897


 
  
 
  
 
  
 
  
 
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值