輸入n個元素組成的序列S,你需要找出一個乘積最大的連續子序列。如果這個最大的乘積不是正數,應輸出0(表示 無解)。
1<=n<=18,-10<=si<=10.
樣例輸入:
3
2 4 -3
5
2 5 -1 2 -1
樣例輸出:
8
20
思路: 枚舉第一個和最後一個
#include<iostream>
#include<cstdio>
using namespace std;
int main()
{
int n,m,i,j;
int a[20],cse=0;
while(scanf("%d",&n)!=EOF&&n)
{
for(i=0;i<n;i++)
scanf("%d",&a[i]);
int k;
long long ans=0;
for(i=0;i<n;i++)//枚舉第一個和最後一個
{
long long sum=1;
for(j=i;j<n;j++)
{
sum=sum*a[j];
if(sum>ans) ans=sum;
}
}
printf("Case #%d: The maximum product is %lld.\n\n",++cse,ans);
}
return 0;
}