例题7-2 最大乘积 UVa 11059

来源:互联网 发布:自学php能找到工作吗 编辑:程序博客网 时间:2024/05/18 02:45

输入n个元素的序列s,找出一个连续序列的最大乘积

分析:数据较小,直接O(n^2)可得出,用 long long

注意:printf输出 long long 时要用%lld ,好像LINUX系统的原因,win系统要用%I64d ,因为我一直用%I64d WA好多次

#include <cstdio>#include <cstring>#include <algorithm>using namespace std;#define LL long longint main(){    int N,s[20];    int p=0,flag=1;    while(scanf("%d",&N)!=EOF){        for(int i=0;i<N;i++)        scanf("%d",&s[i]);        LL  maxn=0;        for(int i=0;i<N;i++){            LL temp=1;            for(int j=i;j<N;j++){                temp*=s[j];                if(temp>maxn) maxn=temp;            }        }        if(maxn<0)maxn=0;       // printf("Case #%d: The maximum product is %lld.\n\n",flag++,maxn);        printf("Case #%d: The maximum product is %I64d.\n\n",flag++,maxn);    }    return 0;}


0 0
原创粉丝点击