您的位置:首页 > 其它

UVA 11059 Maximum Product最大乘积(暴力法)

2017-03-16 23:10 447 查看
分析:暴力枚举求解,最大值不超过10^18,故可用long long 类型求解,枚举起点、终点。

AC代码:

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long LL;
const int maxn=20;
int a[maxn];

LL cal(int i,int j){
LL sum=1;
for(int k=i;k<=j;k++)
sum*=a[k];
return sum;
}
int main(){
int n;
int count1=0;
while(scanf("%d",&n)==1){
for(int i=0;i<n;i++)
scanf("%d",&a[i]);

LL sum=0;
for(int len=1;len<=n;len++){ //长度
for(int i=0;i<n-len+1;i++){ //起点
int j=i+len-1; //终点
LL tmp=cal(i,j);
sum=max(sum,tmp);
}
}
printf("Case #%d: The maximum product is %lld.\n\n",++count1,sum);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: