题解 | #连续子数组的最大乘积#
连续子数组的最大乘积
http://www.nowcoder.com/practice/fd8c819c07c9493887bfac8549c119f4
和上一个连续最大和还是有些区别的哇
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int lg = sc.nextInt();
if(lg==1){
System.out.print(sc.nextInt());
return ;
}
//存储大于0的
int a=1,b=1;
//存储小于0的
int a1=1,b1=1;
int max=-101;
for(int i=1;i<=lg;i++){
int temp = sc.nextInt();
if(temp<0){
a =Math.max(temp,b1*temp);
a1 = Math.min(temp,b*temp);
b=a;
b1=a1;
}else{
a = Math.max(temp,b*temp);
a1 = Math.min(temp,b1*temp);
b=a;
b1=a1;
}
max = max>a?max:a;
}
System.out.print(max);
}
}