题目要求:输入n个元素组成的序列s,你需要找出一个乘积最大的连续子序列,如果这个最大的乘积不是正数,英输出-1(无解)。1<=n<=18,-10<=Si<=10. 我的采用的是暴力枚举: 样例输入: 3 2 4 -3 5 2 5 -1 2 -1 样例输出: 8
20
#include<algorithm>#include<string.h> #include<iostream>using namespace std;intmain(void){ int n; cin >> n; double *p = new double[n]; for (int i = 0; i<n; i++) cin >> p[i]; double q[100]; int k = 0; memset(q, 0, sizeof(q)); double sum = 1.0; for (int i = 0; i < n; i++) { sum = p[i]; for (int j = i + 1; j < n; j++) { sum = sum*p[j]; q[k++] = sum; } } sort(q, q + k); if (q[k-1]<0) cout << -1; else cout << q[k-1];}新闻热点
疑难解答