输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。1n
18,-10
Si
10。
解题方法:暴力破解,列出所有子串长度,然后搜索,但是仅限这种题,因为长度很小,所以可以不考虑超时。
#include<iostream>
#include <string>
#include <string.h>
#include<vector>
#include<stack>
#include<queue>
#include<stdio.h>
#include<stdlib.h>
#include<iomanip>
using namespace std;
int main()
{
int n;
int s[20];
#ifdef LOCAL
freopen("data.in", "r", stdin);
freopen("data.out", "w", stdout);
#endif
int kase = 0;
while (scanf("%d", &n) != EOF) {
for (int i = 0; i < n; i++) {
cin >> s[i];
}
long long int maxt=0;
for(int z=1;z<=n;z++){
for (int i = 0; i+z <= n; i++) {
long long int temp = 1;
for (int j = i; j <i+ z; j++) {
temp *= s[j];
}
maxt = max(maxt, temp);
}
}
printf("Case #%d: The maximum product is %lld.\n\n", ++kase, maxt);
}
//system("pause");
return 0;
}