最大连续子序列乘积_1501

来源:互联网 发布:github下载源码怎么用 编辑:程序博客网 时间:2024/05/29 10:00
题目描述:

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

输入:

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

输出:

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

样例输入:
7-2.5 4 0 3 0.5 8 -15-3.2 5 -1.6 1 2.55-1.1 2.2 -1.1 3.3 -1.1
样例输出:
12648.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