LanQiaoTestCodes/寻找三个数的最大乘积.java

37 lines
948 B
Java
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package LanQiaoOJ;
import java.util.Scanner;
/*题目描述
实现一个算法在数组中找到 3 个数字的最大乘积。介绍如下:
例如数组 [5, -2, 3, 1, -1, 4] 中 3 个数字的最大乘积为 60。
输入描述
第一行为数字N (3≤N≤1000),表示数组元素的个数。
第二行为数组元素 Ai1000≤Ai≤1000。
输出描述
输出一行,为 3 个数字的最大乘积。
*/
public class 寻找三个数的最大乘积 {
static int N=1010;
static int []a=new int [N];
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
for(int i=0;i<n;i++) {
a[i]=sc.nextInt();
}
int ans=(int)-1e9;//考虑数组中有负数的情况——极限情况
for(int i=0;i<n;i++) {
for(int j=i+1;j<n;j++) {
for(int k=j+1;k<n;k++) {
ans=Math.max(ans, a[i]*a[j]*a[k]);
}
}
}
System.out.println(ans);
}
}