您的位置:首页 > 其它

UVa 11059:Maximum Product(水题)

2015-09-04 12:38 537 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=841&page=show_problem&problem=2000

题意:输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。(1≤n≤18,−10≤Si≤10)(1 \le n \le 18, -10 \le S_i \le 10)。(本段摘自《算法竞赛入门经典(第2版)》

分析:水题,直接暴力枚举起点终点即可。

代码:

#include <fstream>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <stack>
#include <sstream>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <vector>
#include <set>
#include <string>
#include <vector>

using namespace std;

const int maxn = 2000 + 5;

int n, C;
long long ans, tmp;
long long a[20];

int main()
{
while (~scanf("%d", &n))
{
ans = 0;
for (int i = 0; i < n; ++i)
scanf("%lld", &a[i]);
for (int i = 0; i < n; ++i)
for (int j = i; j < n; ++j)
{
tmp = 1;
for (int k = i; k <= j; ++k)
tmp *= a[k];
ans = max(ans, tmp);
}
printf("Case #%d: The maximum product is %lld.\n\n", ++C, ans);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: