输入描述:
首先输入n,代表数组内整数的个数
再依次输入整数
输入用例:
61
3472 -7098 -9281 7789 7955 6101 5051 7778 3090 7423 -7151 5652 1595 -8094 677 -8324 8347 -2482 9313 -9338 -3157 8559 6945 3618 3087 121 -8468 3225 1356 6939 2799 -7231 -6309 -5453 633 -8689 -4776 2714 -2743 -1409 5918 -3333 1803 8330 -2206 -6117 -4486 -7903 -4375 -3739 2897 8056 -5864 -522 7451 -4541 -2813 5790 -532 -6517 925
public class test {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long[] arr = new long[n];
for (int i = 0; i < arr.length; i++) {
arr[i] = sc.nextInt();
}
max(arr);
}
private static void max(long[] arr) {
// TODO Auto-generated method stub
long max1 = 0, max2 = 0, max3 = 0;
long min1 = 0,min2 = 0;
long max;
for (long i : arr) {
if(i!=0){
if(i>max1){
max3 = max2;
max2 = max1;
max1 = i;
}else if(i>max2){
max3 = max2;
max2 = i;
}else if(i>max3){
max3 = i;
}else if(i<min1 ){
min2 = min1;
min1 = i;
}else if(i>min1 && i<min2){
min2 = i;
}
}else continue;
}
System.out.println(max = Math.max(max1*max2*max3,max1*min1*min2));
}
}
运行结果: