最大乘积 Maximum Product
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=84562#problem/B
解题思路:
题目意思是输入n个元素组成的序列S,找出一个乘积最大的连续子序列。若这个数不是正数,则输出0(表示无解)。分析 ,连续子序列有两个要素:起点和终点,因此只需要枚举起点和终点即可。分析最大可能的乘积不会超过10的18次方,所以用 long long 来存储即可。
程序代码:
#include <cstdio>
using namespace std;
int a[];
int main()
{
int n,Case=;
while( scanf("%d",&n)==&&n)
{
for(int i=;i<n;i++)
scanf("%d",&a[i]);
long long maxn=;
for(int i=;i<n;i++)
{
long long temp=;
for(int j=i;j<n;j++)
{
temp*=a[j];
if(temp>maxn) maxn=temp;
}
}
printf("Case #%d: The maximum product is %lld.\n\n",Case++,maxn);
}
return ;
}