最大积res只有两种情况,res=Max(最大的数 次大的数
第三大的数,最大的数
最小的数
次小的数)
举例说明的话就是,如:[1,2,3,4],[-4,-3,-2,-1],[-2,-1,0,100,200,300]都是取第一种情况
如[-200,-100,0,1,2,3]就是取第二种情况
public class Main{
Scanner scan=new Scanner(System.in);
public static void main(String[] args) {
MainmaxProduct=new Main();
maxProduct.max_product();
}
static Comparator<Integer> cmp = new Comparator<Integer>() {
public int compare(Integer e1, Integer e2) {
return e2 - e1;
}
};
private void max_product(){
int n=scan.nextInt();
int [] arr=new int[n];
PriorityQueue<Integer> Descending=new PriorityQueue<>(cmp);//降序
PriorityQueue<Integer> Ascending=new PriorityQueue<>();
for(int i=0;i<n;i++){
int temp=scan.nextInt();
Ascending.add(temp);
Descending.add(temp);
}
long max1=Descending.poll();
long max2=Descending.poll();
long max3=Descending.poll();
long min1=Ascending.poll();
long min2=Ascending.poll();
long res=Math.max(max1*max2*max3,max1*(min1*min2));
System.out.println(res);
}
}
京公网安备 11010502036488号