题目链接:
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 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;
}

本文介绍了一种解决最大连续子序列乘积问题的方法。通过两重for循环暴力搜索算法来寻找给定序列中乘积最大的连续子序列,并考虑了特殊情况下的输出。代码使用C++实现。
384

被折叠的 条评论
为什么被折叠?



