给定一个double类型的数组arr,其中的元素可正、可负、可0,返回子数组累乘的最大乘积。例如,arr=[-2.5, 4, 0, 3, 0.5, 8, -1],子数组[3, 0.5, 8]累乘可以获得最大的乘积12,所以返回12
[要求]
时间复杂度为,空间复杂度为
第一行一个整数N。表示数组长度。
接下来一行N个浮点数表示数组内的数
输出一个浮点数表示答案,保留到小数点后两位
7 -2.5 4 0 3 0.5 8 -1
12.00
#include <bits/stdc++.h> using namespace std; int main(){ int n; cin>>n; double a[n], Max=1, Min=1, s=0; for(int i=0;i<n;i++){ cin>>a[i]; double s1 = Max * a[i]; double s2 = Min * a[i]; Max = max(a[i], max(s1, s2)); Min = min(a[i], min(s1, s2)); s = max(s, Max); } printf("%.2f\n", s); return 0; }
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; 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[] strArr = br.readLine().split(" "); double[] nums = new double[n]; for(int i = 0; i < n; i++) nums[i] = Double.parseDouble(strArr[i]); double dmax = 1.0, dmin = 1.0; double max = Double.MIN_VALUE; for(int i = 0; i < n; i++){ if(nums[i] < 0){ // 遇到负数时,需要交换一下到目前为止的最大累乘值和最小累乘值,因为会使得最大的变最小,最小的变最大 double temp = dmax; dmax = dmin; dmin = temp; } dmax = Math.max(dmax * nums[i], nums[i]); dmin = Math.min(dmin * nums[i], nums[i]); max = Math.max(max, dmax); } System.out.println(String.format("%.2f", max)); } }
#include <bits/stdc++.h> using namespace std; int main(){ int n; double num; scanf("%d%lf", &n,&num); double maxX = num; double minX = num; double ans = num; for(int i=1; i<n; i++){ scanf("%lf", &num); double temp = maxX; maxX = max(max(num*maxX, num*minX), num); minX = min(min(num*temp, num*minX), num); ans = max(ans, maxX); } printf("%.2f", ans); return 0; }
import java.util.*; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); double[] arr = new double[n]; for(int i=0;i<n;i++){ arr[i] = scanner.nextDouble(); } double max = 1,min = 1,res = Integer.MIN_VALUE,p=0,q=0; for(int i=0;i<n;i++) { p = max * arr[i]; q = min * arr[i]; max = Math.max(Math.max(p, q), arr[i]); min = Math.min(Math.min(p, q), arr[i]); res = Math.max(res, max); } System.out.print(String.format("%.2f", res)); } }
#include<iostream> #include<vector> #include<algorithm> #include <iomanip> using namespace std; int main() { int N; vector<double> arrIn; cin >> N; for (int i = 0; i<N; i++) { double temp; cin >> temp; arrIn.push_back(temp); } double res = arrIn[0]; double maxc = arrIn[0]; double minc = arrIn[0]; double premax = 0, premin = 0; /*计算以i结尾子数组最大乘积 1,以arr[i-1]结尾的最大乘积premax*arr[i]; 2,以arr[i-1]结尾的最小乘积premin*arr[i]; 两个负数相乘 3,本身arr[i]最大 */ for (int i = 1; i < N; i++) { premax = maxc*arrIn[i]; premin = minc*arrIn[i]; maxc = max(max(premax, premin), arrIn[i]); minc = min(min(premax, premin), arrIn[i]); res = max(res, maxc); } cout << fixed << setprecision(2) << res << endl; return 0; }
#include<bits/stdc++.h> using namespace std; int main() { int n; cin>>n; vector<double>num(n); for(int i=0;i<n;i++) cin>>num[i]; double res=num[0],minn = num[0],maxx = num[0]; for(int i=1;i<n;i++) { double x = maxx; maxx = max(num[i],max(minn*num[i], maxx*num[i])); minn = min(num[i], min(minn*num[i], x*num[i])); res = max(res, maxx); } cout<<fixed << setprecision(2) <<res<<endl; return 0; }
# include <bits/stdc++.h> using namespace std; int main() { int n; cin >> n; vector<double> nums(n); for(int i = 0; i < n; ++i) cin >> nums[i]; double ans = 0; // 一个保存最大的,一个保存最小的。 double maxsum = 1; double minsum = 1; for(int i = 0; i < n; ++i){ // 如果遇到数组的数是负数,那么会导致最大的变最小的,最小的变最大的。因此交换两个的值 if(nums[i] < 0) swap(maxsum, minsum); // 数组元素为正数的情况,最大乘积可能是乘上当前元素,也可能变成元素本身 // 取决于上一个元素为止的最大乘积是正数还是负数 maxsum = max(nums[i], maxsum * nums[i]); // 维护一个最小乘积,在元素 < 0 时使用 minsum = min(nums[i], minsum * nums[i]); ans = ans > maxsum ? ans : maxsum; } printf("%.2f\n", ans); return 0; }
#include <iostream> #include <vector> #include <stack> using namespace std; int main() { int n = 0; cin >> n; vector<double> arr(n, 0); for (int i = 0; i < n; i++) { cin >> arr[i]; } if (n == 0) { printf("%.2f", 0.00); return 0; } double p1 = 0, p2 = 0, p3 = 0; double maxValue = arr[0], minValue = arr[0], res = arr[0]; for (int i = 1; i < n; i++) { // 可能值1: 过去的最大连续值 * 当前值 p1 = maxValue * arr[i]; // 可能值2: 过去的最小连续值 * 当前值 p2 = minValue * arr[i]; // 可能值3: 当前值 p3 = arr[i]; // 3个可能值取一个最大值 maxValue = max(max(p1, p2), p3); minValue = min(min(p1, p2), p3); res = max(maxValue, res); } printf("%.2f", res); return 0; }
public class Main { public static void main(String[] args) { Scanner cin = new Scanner(System.in); int t = cin.nextInt(); cin.nextLine(); String[] line = cin.nextLine().split(" "); double[] nums = new double[t]; for (int i=0; i < t; i++) { nums[i] = Double.parseDouble(line[i]); } double dp_min_i = nums[0]; double dp_max_i = nums[0]; double dp_min_ii = Double.MAX_VALUE; double dp_max_ii = 0; double ret = nums[0]; for (int i=1; i < t; i++) { double tmp_1 = dp_min_i * nums[i]; double tmp_2 = dp_max_i * nums[i]; dp_min_ii = Math.min(tmp_1, Math.min(tmp_2, nums[i])); dp_max_ii = Math.max(tmp_1, Math.max(tmp_2, nums[i])); ret = Math.max(dp_max_ii, ret); dp_min_i = dp_min_ii; dp_max_i = dp_max_ii; } System.out.printf("%.2f\n", ret); } }
#include<iostream> using namespace std; const int N = 1e5+10; double a[N]; inline double max(double a,double b ,double c) { return max(a,max(b,c)); } inline double min(double a,double b,double c) { return min(a,min(b,c)); } int main() { int n; cin>>n; double res=0; double t1=1,t2 = 1; for(int i=0;i<n;++i) { cin>>a[i]; } for(int i=0;i<n;++i) { double s1 = t1*a[i]; double s2 = t2*a[i]; t1 = max(a[i],s1,s2); t2 = min(a[i],s1,s2); res = max(res, t1); } printf("%.2lf" ,res); }