[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;
}