[UVA11059]Maximum Product
1.题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000
2.题意分析:给出一个序列,求解这个序列中最大连续累乘的子序列中,最大的值为多少,如果都为负数,则输出0.
3.解题思路:整个元素最多有18个,大小最大10.所以要开到long long。然后列举所有长度即可。另外要注意输出格式,每个样例都要输出一个空白行(主要是这个空白行害得我改了半天没发现。。。)
4.具体执行方法:因为原始数据并不大,可以开到int,算乘积时要注意long long ,计算时用双循环截取数组片段,一个控制头部,一个控制尾部。然后算出乘积比较即可。
如果上面链接打开缓慢,也可移步此处提交:https://cn.vjudge.net/problem/UVA-11059
以下是代码
#include <bits/stdc++.h>
#include <algorithm>
#include <cmath>
using namespace std;
int x,y,z;
long long int calc(int s[],int m)
{
int j;
long long int ans=0;
for (int x=0;x<m;x++)
{
for (int y=x;y<m;y++)
{
long long ch=1;
for (int z=x;z<=y;z++)
{
ch*=s[z];
}
ans=max(ans,ch);
}
}
return ans;
}
int main()
{
int a;
int k=0;
while(scanf("%d",&a)!=EOF)
{
int i;
int *s;
k++;
s=(int *)malloc(sizeof(int)*a);
memset(s,0,sizeof(s));
for(i=0;i<a;i++)
scanf("%d",&s[i]);
printf("Case #%d: The maximum product is %lld.\n\n",k,calc(s,a));
}
return 0;
}