码迷,mamicode.com
首页 > 其他好文 > 详细

最大连续子序列乘积

时间:2014-09-09 21:21:09      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   java   ar   for   div   sp   

假设数组为a[],直接利用动归来求解,考虑到可能存在负数的情况,我们用Max来表示以a结尾的最大连续子串的乘积值,用Min表示以a结尾的最小的子串的乘积值,那么状态转移方程为:
Max=max{a, Max[i-1]*a, Min[i-1]*a};
Min=min{a, Max[i-1]*a, Min[i-1]*a};
初始状态为Max[1]=Min[1]=a[1]

 Java实现(在oj上测试有RuntimeError错误)

 1 package longest_multiple;
 2 
 3 import java.util.Scanner;
 4 
 5 
 6 public class Main {
 7     
 8     final static int N = 100001;
 9     static double a[] = new double[N];
10     static double Min[] = new double[N];
11     static double Max[] = new double[N];
12 
13     static double max(double a, double b) {
14         return (a > b) ? a : b;
15     }
16 
17     static double min(double a, double b) {
18         return (a < b) ? a : b;
19     }
20 
21     static double longest_multile(double a[], int n) {
22 
23         Max[0] = Min[0] = a[0];
24         double max_value = a[0];
25         for (int i = 1; i < n; i++) {
26 
27             Max[i] = max(max(a[i], Max[i - 1] * a[i]), Min[i - 1] * a[i]);
28             Min[i] = min(min(a[i], Max[i - 1] * a[i]), Min[i - 1] * a[i]);
29             if (max_value < Max[i]) {
30                 max_value = Max[i];
31             }
32         }
33         return max_value;
34     }
35 
36     public static void main(String[] args) {
37         Scanner sc = new Scanner(System.in);
38         while (sc.hasNext()) {
39 
40             int n = sc.nextInt();
41 
42             for (int i = 0; i < n; i++) {
43                 a[i] = sc.nextDouble();
44             }
45             double res = longest_multile(a, n);
46             if (res < 0) {
47                 System.out.println("-1");
48             } else {
49                 if (res - (int) res <= 1e-5) {
50                     System.out.println((int) res);
51                 } else {
52 
53                     System.out.printf("%.2lf\n", res);
54                 }
55             }
56         }
57     }
58 
59 }

C语言实现

 1 #include <stdio.h>
 2  
 3 #define LEN 100000
 4  
 5 int N;
 6 double data[LEN];
 7 double max[LEN];
 8 double min[LEN];
 9  
10 double Max(double a, double b){
11     return (a > b) ? a : b;
12 }
13  
14 double Min(double a, double b){
15     return (a < b) ? a : b;
16 }
17  
18 double MMS(){
19     int i;
20     double ans;
21     max[0] = min[0] = data[0];
22     ans = data[0];
23     for (i = 1; i < N; ++i){
24         max[i] = Max(Max(data[i], max[i-1]*data[i]), min[i-1]*data[i]);
25         min[i] = Min(Min(data[i], max[i-1]*data[i]), min[i-1]*data[i]);
26         ans = Max(ans, max[i]);
27     }
28     return ans;
29 }
30  
31 int main(void){
32     int i;
33     double ans;
34     while (scanf("%d", &N) != EOF){
35         for (i = 0; i < N; ++i)
36             scanf("%lf", &data[i]);
37         ans = MMS();
38         if (ans < 0)
39             printf("-1\n");
40         else if (ans - (int)ans <= 1e-5)
41             printf("%lld\n", (int)ans);
42         else
43             printf("%.2lf\n", ans);
44     }
45  
46     return 0;
47 }

 

最大连续子序列乘积

标签:style   blog   color   io   java   ar   for   div   sp   

原文地址:http://www.cnblogs.com/xxwacm/p/3963277.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!