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;
}
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;
}
相关文章推荐
- 暴力求解——最大乘积 Maximum Product,UVa 11059
- 最大乘积(Maximum Product,UVa 11059)
- 最大乘积(Maximum Product,UVA 11059)
- 例题7-2 最大乘积(Maximum Product, UVa 11059)
- 最大乘积(Maximum Product,UVA 11059)
- 最大乘积(Maximum Product,UVA 11059)
- 最大乘积(Maximum Product,UVa 11059)
- 最大乘积(Maximum Product,UVa 11059)
- UVa 11059 最大乘积 java 暴力破解
- [Day Zero] 7.1-2 最大乘法(Maximum Product,UVa 11059)
- LeetCode题库解答与分析——#152. 乘积最大子序列MaximumProductSubarray
- LeetCode-628:Maximum Product of Three Numbers (数组三元素最大乘积)
- LeetCode:152_Maximum Product Subarray | 最大乘积连续子数组 | Medium
- 例题7-2 最大乘积 UVa 11059
- 11059 - Maximum Product(暴力)
- Maximum Product UVA - 11059
- [LeetCode] Maximum Product Subarray 求最大子数组乘积
- 【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大
- UVa 11059 最大乘积
- 最大乘积(Maximum Product)