有正,负,0问题,所以动归要最小/最大都考虑
O(n)
O(1)
import java.util.Scanner; public class Main { public static Scanner sc = new Scanner(System.in); public static void main(String[] args) { int n = sc.nextInt(); double[] arr = new double[n]; for(int i=0;i<n;i++){ arr[i] = sc.nextDouble(); } double res = 0; double preMin = 0; double preMax = 0; res = arr[0]; preMin = arr[0]; preMax = arr[0]; for(int i=1;i<n;i++){ double tmpMin = preMin; double tmpMax = preMax; preMin = Math.min(tmpMin * arr[i], tmpMax * arr[i]); preMin = Math.min(preMin, arr[i]); preMax = Math.max(tmpMax * arr[i], tmpMin * arr[i]); preMax = Math.max(preMax, arr[i]); res = Math.max(res, preMax); } System.out.print(String.format("%.2f", res)); } } /* 7 -2.5 4 0 3 0.5 8 -1 12.00 */