UVa 11059 Maximum Product

来源:互联网 发布:淘宝商品排名软件 编辑:程序博客网 时间:2024/04/28 09:13

题目链接:
http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=27946

题目概述:

输入n个元素组成的序列S,找出其中乘积最大的连续子序列。如果最大的不是正数,输出0

思路:

直接两重for暴力搜索

这里写图片描述
这里写图片描述

/*************************************************************************    > File Name: UVa_11059.cpp    > Author: dulun    > Mail: dulun@xiyoulinux.org    > Created Time: 2016年03月18日 星期五 20时24分10秒 ************************************************************************/#include<iostream>#include<stdio.h>#include<cstring>#include<cstdlib>#include<algorithm>#define LL long longusing namespace std;const int N = 50086;int a[20];int cnt = 0;int n;void f(){    LL ans = 0;    for(int i = 1; i <= n; i++)    {        LL sum = 1;        for(int j = i; j <= n; j++)        {            sum *= a[j];            ans = max(ans, sum);        }    }    printf("Case #%d: The maximum product is %lld.\n\n", ++cnt, ans);}int main(){    while(~scanf("%d", &n))    {        for(int i = 1; i <= n; i++)         {            scanf("%d", &a[i]);        }        f();    }    return 0;}
0 0
原创粉丝点击