UVa 11059 Maximum Product

题意:给出n个数组成的序列,求乘积最大的连续子序列

看的紫书,因为n最大为18,每个数最大为10,所以10^18用long long 能够存下, 直接枚举起点和终点找最大值就可以了

 1 #include<iostream>  

 2 #include<cstdio>  

 3 #include<cstring> 

 4 #include <cmath> 

 5 #include<stack>

 6 #include<vector>

 7 #include<map> 

 8 #include<queue> 

 9 #include<algorithm>  

10 #define mod=1e9+7;

11 using namespace std;

12 

13 typedef long long LL;

14 int a[105];

15 

16 int main(){

17     LL ans,maxn;

18     int i,j,n,cas=1;

19     while(scanf("%d",&n)!=EOF){

20         for(i=1;i<=n;i++) scanf("%d",&a[i]);

21         maxn=-1000;

22         

23         for(i=1;i<=n;i++){

24             ans=a[i];

25             maxn=max(ans,maxn);

26             for(j=i+1;j<=n;j++){

27                 ans*=a[j];

28                 maxn=max(ans,maxn);

29             //    printf("ans=%d\n",ans);

30                 

31             }

32         }

33         if(maxn<0) maxn=0;

34         printf("Case #%d: The maximum product is %lld.\n\n",cas++,maxn);      

35     }

36     return 0;

37 }
View Code

 

你可能感兴趣的:(uva)