【搜索】训练题J-Maximum Product

题意

给了n个数字,要求就是求出这组数按顺序相乘的最大值,如果结果不是正整数,那么答案为0

思路

枚举每一个点,计算往后相乘的结果,并且维护一个数字max,当相乘的结果ans大于max的时候,将ans的值赋给max。本题需要注意的是每个case结束之后要空一行

代码

#include<iostream>
#include<cmath>
#include<stack>
#include<map>
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<algorithm>

#define Endl "\n"
typedef long long ll;
const int maxn=20;
const int mod=1e9+7;
using namespace std;
int a[maxn];
int main()
{
	int test =1;
		
		ll n;
	
		while(cin>>n)
		{
			ll max=0,ans;
			for(int i=0;i<n;i++)
				cin>>a[i];
			for(int i=0;i<n;i++)
			{
				ans=1;
				for(int j=i;j<n;j++)
				{
					ans*=a[j];
					if(ans>max)
						max=ans;
				}
			}
		
		cout<<"Case #"<<test;
		test++;
		cout<<": The maximum product is "<<max<<"."<<Endl<<Endl;
	}
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值