Uva 11059 Maximum Product(简单枚举)

来源:互联网 发布:mac 042迅雷链接 编辑:程序博客网 时间:2024/05/29 05:01

·思路:枚举对象:子序列的起点和终点

#include <cstdio>#include <cmath>#include <cstring>using namespace std;const int maxn=20;const long long INF=(long long)pow(10,18);int S[maxn];int main(){    int n,kase=1;    while(scanf("%d",&n)!=EOF){        memset(S,0,sizeof(S));        for(int i=0;i<n;i++) scanf("%d",&S[i]);        int st,en;        long long maxm=-INF;        for(st=0;st<n;st++){            for(en=st;en<n;en++){                long long multi=1;                for(int i=st;i<=en;i++) multi*=S[i];                maxm=maxm<multi?multi:maxm;            }        }        if(maxm<=0) printf("Case #%d: The maximum product is %lld.\n\n",kase++,0);        else printf("Case #%d: The maximum product is %lld.\n\n",kase++,maxm);    }    return 0;} 

注意点:1. 由于最大乘积是10^18,因此不能用int来存储,而用long long。同时,注意long long 变量输出时是%lld。(比如最后两行,写成了%d,于是在用18个10测试时出来的答案是没有意义的长串)。
2.由于本题规定了乘积需要大于0,所以maxm可以不初始为-INF,而初始为0。

0 0
原创粉丝点击