代码实现:
import java.util.*; public class Main{ Scanner scan=new Scanner(System.in); public static void main(String[] args) { Main maxProduct=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); } }