UVa 11059 Maximum Product

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

题目概述:

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

思路:

直接两重for暴力搜索

UVa 11059 Maximum Product_第1张图片

/************************************************************************* > File Name: UVa_11059.cpp > Author: dulun > Mail: [email protected] > Created Time: 2016年03月18日 星期五 20时24分10秒 ************************************************************************/

#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#define LL long long
using 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;
}

你可能感兴趣的:(搜索,uva)