public static void main(String[] args) {
PDD1();
}
public static void PDD1(){
long sum = 1;
Scanner scanner = new Scanner(System.in);
int n = scanner.nextInt();
List<Long> arrayList = new ArrayList<>();
for (int i = 0; i < n; i++) {
arrayList.add(scanner.nextLong());
}
arrayList.sort((o1, o2) -> {
Long tmp = o2-o1;
return tmp.intValue();
}
);
if (arrayList.get(1)*arrayList.get(2)<arrayList.get(arrayList.size()-1)*arrayList.get(arrayList.size()-2)){
sum = arrayList.get(0)*arrayList.get(arrayList.size()-1)*arrayList.get(arrayList.size()-2);
}else {
sum = arrayList.get(0)*arrayList.get(1)*arrayList.get(2);
}
System.out.println(sum);
} import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Comparator;
import java.util.PriorityQueue;
/**
* @Author: coderjjp
* @Date: 2020-05-13 11:46
* @Description: 最大乘积
* @version: 1.0
*/
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
int n = Integer.parseInt(br.readLine());
String[] line2 = br.readLine().split(" ");
int num[] = new int[n];
for (int i = 0; i < n; i++)
num[i] = Integer.parseInt(line2[i]);
//维护一个容量为3的小顶堆,存储最大的三个数,
PriorityQueue<Integer> min = new PriorityQueue<>(3);
//维护一个容量为2的大顶堆,存贮最小的两个数
PriorityQueue<Integer> max = new PriorityQueue<>(2,(o1, o2) -> o2 - o1);
for (int i = 0; i < n; i++){
if (min.size() < 3) min.offer(num[i]);
else {
if (num[i] > min.peek()){
min.poll();
min.offer(num[i]);
}
}
if (max.size() < 2) max.offer(num[i]);
else {
if (num[i] < max.peek()){
max.poll();
max.offer(num[i]);
}
}
}
int max1 = min.poll(), max2 = min.poll(), max3 = min.poll();
int min1 = max.poll(), min2 = max.poll();
System.out.println(Math.max(1l*max1*max2*max3, 1l*max3*min1*min2));
}
} Scanner s = new Scanner(System.in); int line = s.nextInt(); int[] shuzu = new int[line]; for(int i=0;i<shuzu.length;i++) { shuzu[i]=s.nextInt(); } Arrays.sort(shuzu); long a = shuzu[shuzu.length-3]*shuzu[shuzu.length-1]*shuzu[shuzu.length-2]; long b = shuzu[shuzu.length-1]*shuzu[0]*shuzu[1]; if(a>=b) { System.out.println(a); } else { System.out.println(b); }
ort java.util.Arrays;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
int n = scan.nextInt();
long[] arr = new long[n];
for(int i =0; i < n; i++) {
arr[i] = scan.nextLong();
}
Arrays.sort(arr);
int len = arr.length;
long result1 = arr[len-1]*arr[len-2]*arr[len-3];
long result2 = arr[0]*arr[1]*arr[len-1];
System.out.println(Math.max(result1, result2));
}
}
//利用包装类Collections提供的sort方法。 import java.util.ArrayList; import java.util.Collections; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); if (n < 3) { return; } ArrayList<Long> list = new ArrayList<>(); for (int i = 0; i < n; i++) { list.add(scanner.nextLong()); } Collections.sort(list); long max = list.get(list.size() - 1) * list.get(list.size() - 3) * list.get(list.size() - 2); long min = list.get(0) * list.get(1) * list.get(list.size() - 1); max = max > min ? max : min; System.out.println(max); } }
import java.util.Scanner;
import java.util.Comparator;
import java.util.PriorityQueue;
public class Main {
public static long getMax(long[] arr){
if (arr.length == 3)
return arr[0]*arr[1]*arr[2];
PriorityQueue<Long> qmax = new PriorityQueue<>();
PriorityQueue<Long> qmin = new PriorityQueue<>((Long o1, Long o2) -> {
return o2>o1? 1 : -1;
});
for (int i = 0; i < 3; i++) {
qmax.add(arr[i]);
qmin.add(arr[i]);
}
for (int i = 3; i < arr.length; i++) {
if (qmax.peek()<arr[i]){
qmax.poll();
qmax.add(arr[i]);
}
if (qmin.peek()>arr[i]){
qmin.poll();
qmin.add(arr[i]);
}
}
long c = qmax.poll(), b = qmax.poll(), a = qmax.poll();
qmin.poll();
long e = qmin.poll(), f = qmin.poll();
return a*e*f>a*b*c?a*e*f:a*b*c;
}
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (scanner.hasNext()){
int n = scanner.nextInt();
long[] arr = new long[n];
for (int i = 0; i < n; i++) {
arr[i] = scanner.nextLong();
}
System.out.println(getMax(arr));
}
}
} import java.util.Scanner;
public class Main {
public static long process(long[] arr) {
long neg1 = 0;
long neg2 = 0;
long pos1 = 0;
long pos2 = 0;
long pos3 = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] < 0) {
if (arr[i] < neg1) {
neg2 = neg1;
neg1 = arr[i];
} else if (arr[i] < neg2) {
neg2 = arr[i];
}
} else {
if (arr[i] > pos3) {
pos1 = pos2;
pos2 = pos3;
pos3 = arr[i];
} else if (arr[i] > pos2) {
pos1 = pos2;
pos2 = arr[i];
} else if (arr[i] > pos1) {
pos1 = arr[i];
}
}
}
long res1 = neg1 * neg2 * pos3;
long res2 = pos1 * pos2 * pos3;
return res1 > res2 ? res1 : res2;
}
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 < n; i++) {
arr[i] = sc.nextLong();
}
System.out.println(process(arr));
}
}
先自己设置数组的长度a,再通过循环a次,把每个值添加到数组中。通过Arrays.sort(数组名称); 对数组进行排序,然后再统计 数组中 值为负数的数量 是否大于等于 2 ,因为负负得正 , 再比较 (最大的三个数的乘积) 与 (最小的两个负数和最大的整数的乘积)输出较大的值。如果 数组中 值为负数的数量 小于2,则直接输出 最大的三个正整数的乘积。
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int a = in.nextInt();
int i = 0 ;
long[] array = new long[a];
for (; i < a ; i++) {
array[i] = in.nextInt();
int k = 0 ;
for (int j = 0; j < i ; j++) {
if (array[j] < 0){
k++;
}
}
Arrays.sort(array);
//System.out.println(Arrays.toString(array));
if (k >= 2){
//System.out.println(array[0] + "," + array[1] + "," + array[i-1]);
long x = array[0] * array[1] * array[i-1];
//System.out.println(x);
long y = array[i-1] * array[i-2] * array[i-3];
//System.out.println(y);
if (x>y) {
System.out.println(x);
}else{
System.out.println(y);
}
}else {
long c = array[i-1] * array[i-2] * array[i-3];
System.out.println(c);
}
}
} }
import java.util.Scanner;
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
long[] array=new long[n];
for(int i=0;i<n;i++){
array[i]=sc.nextLong();
}
getTheMostValue(array,n);
}
public static void getTheMostValue(long[] num,int len){
long max1=0;long max2=0;long max3=0;long min1=0;long min2=0;
for(int i=0;i<len;i++){
if(num[i]>max1){
max3=max2;
max2=max1;
max1=num[i];
}else if(num[i]>max2){
max3=max2;
max2=num[i];
}else if(num[i]>max3){
max3=num[i];
}else if(num[i]<min1){
min2=min1;
min1=num[i];
}else if(num[i]>min1&&num[i]<min2){
min2=num[i];
}
}
long max=Math.max(max1*max2*max3,max1*min1*min2);
System.out.println(max);
}
}
public static void main(String[] args)throws Exception { String str;思路:
1.先排序
2.分两种情况:第一种数组中有正数也有负数,并且有两个以上负数,则排好序后前两个数和最后一个数相加;
第二种,数组后三个数相加,对比第一第二种情况那个值大就输出哪个。
3.使用长整形long定义数组防止溢出。
importjava.util.*;
publicclassMain{
publicstaticvoidsort1(longb[]){
for(inti=0;i<b.length-1;i++){
for(intj=0;j<b.length-i-1;j++){
if(b[j]>b[j+1]){
longtemp;
temp=b[j+1];
b[j+1]=b[j];
b[j]=temp;
}
}
}
}
publicstaticvoidmain(String[] args) {
Scanner input = newScanner(System.in);
intlen = input.nextInt();
inti;
long[] a = newlong[len];
for( i=0;i<a.length;i++) {
a[i]=input.nextInt();
}
sort1(a);
longsum=0;
longsum1=0;
sum = a[a.length-1]*a[a.length-2]*a[a.length-3];
sum1 = a[0]*a[1]*a[a.length-1];
if(sum<sum1) {
sum=sum1;
}
System.out.println(sum);
}
}
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int sum = in.nextInt();
List<Integer> set = new ArrayList<>();
for(int i=0;i<sum;i++) {
set.add(in.nextInt());
}
set.sort((a,b)->{
return a>b?1:-1;
});
BigDecimal b1=new BigDecimal(set.get(0));
BigDecimal b2=new BigDecimal(set.get(1));
BigDecimal b3=new BigDecimal(set.get(set.size()-1));
BigDecimal b4=new BigDecimal(set.get(set.size()-2));
BigDecimal b5=new BigDecimal(set.get(set.size()-3));
BigDecimal m1=b1.multiply(b2).multiply(b3);
BigDecimal m2=b3.multiply(b4).multiply(b5);
if(m1.compareTo(m2)>0) {
System.out.println(m1);
}else {
System.out.println(m2);
}
}
} BigDecimal 计算大数据,这里用的行数比较多