标签:
题目大意:
就是说给你n个数字,然后求出连续序列的最大和。如果ans为负数就输出0,要么就输出这个ans。
解题思路:
其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题。
唯一的不同就是每次都要初始化t = 1, t*=a[j]。注意long long。因为最多有18个数字,且每个数字的
最大值为10,那么他们的乘积是不会超过10^18的。
代码:
# include<cstdio> # include<iostream> # include<algorithm> # include<functional> # include<cstring> # include<string> # include<cstdlib> # include<iomanip> # include<numeric> # include<cctype> # include<cmath> # include<ctime> # include<queue> # include<stack> # include<list> # include<set> # include<map> using namespace std; const double PI=4.0*atan(1.0); typedef long long LL; typedef unsigned long long ULL; # define inf 999999999 # define MAX 23 int a[MAX]; int n; int main(void) { int icase = 1; while ( cin>>n ) { for ( int i = 1;i <= n;i++) { cin>>a[i]; } LL ans = 0; for ( int i = 1;i <= n;i++ ) { LL t = 1; //ans = max(ans,t); for ( int j = i;j <= n;j++ ) { t*=a[j]; ans = max(ans,t); } } printf("Case #%d: The maximum product is %lld.\n\n",icase++,ans); } return 0; }
UVA 11059 Maximum Product(枚举start+end)
标签:
原文地址:http://www.cnblogs.com/wikioibai/p/4391362.html