package pandy.a;
import java.util.Scanner;
/**
* @Author: Pandy
* @Date: 2019/4/23 20:34
* @Version 1.0
* 输入一个数组求出乘积最大的三个数的乘积
* 其中可能包括 正数 负数 0
*/
public class Test {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int len = in.nextInt();
long[] arr = new long[len];
for (int i = 0;i<len;i++){
arr[i] = in.nextLong();
}
Long test1 = test(arr);
System.out.println(test1);
}
public static Long test(long[] arr){
long max1 = 1;
long max2 = 1;
long max3 = 1;
long min1 = 1;
long min2 = 1;
for (long num : arr) {
if (num > max1){
max3 = max2;
max2 = max1;
max1 = num;
}else if (num > max2){
max3 = max2;
max2 = num;
}else if(num > max3){
max3 = num;
}
if (num < min1){
min2 = min1;
min1 = num;
}else if(num < min2){
min2 = num;
}
}
return Math.max(max1*max2*max3,min1*min2*max1);
}
}
复制代码
输入数组求出乘积最大的三个数的乘积
最新推荐文章于 2022-11-24 23:31:18 发布