您的位置:首页 > 其它

[UVA11059]Maximum Product 暴力求解入门

2017-02-26 10:27 218 查看
[UVA11059]Maximum Product[暴力]

题意:给出一个序列,问这个序列中最大连续累乘的子序列中,最大的值为多少,如果都为负数,则输出0.

代码:#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;

const int maxn = 100;
long long num[maxn];
vector<int>v;

bool cmp(long long a,long long b)
{
return a>b;
}
int main()
{
int n;
int cnt = 0;
while(cin>>n)
{
memset(num,0,sizeof(num));
for(int i=0;i<n;i++)
{
cin>>num[i];
}
long long vis[maxn];
long long t = 1;
for(int i=0;i<n;i++)
{
t *=num[i];
v.push_back(t);
}
sort(v.begin(),v.end(),cmp);
long long temp = v.front();
if(temp <=0)
{
cout<<'0'<<"\n";
}
else {
cout<<"Case #"<<++cnt<<": The maximum product is "<<temp<<".\n";
}
v.clear();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: