UVA - 11059 Maximum Product C++

本文探讨了一个关于寻找数组中具有最大乘积的连续子序列的问题,并提供了一种使用动态规划方法解决该问题的代码实现。代码通过遍历数组并不断更新累积乘积来寻找最优解。

题目:https://odzkskevi.qnssl.com/f291a3cd7af74cfc9f9770e2dde91718?v=1532124336

思路:

其实我不太清楚为什么我dp没过。。

代码:

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
const int INF = 0x3f3f3f3f;
int main()
{
    int n;
    int tot=1;
    while(scanf("%d",&n)!=EOF)
    {
        int a[20];
        for(int i=0;i<n;i++)
        {
            scanf("%d",&a[i]);
        }
        long long int ans=-INF;
        long long int p;
        for(int i=0;i<n;i++)
        {
            p=1;
            for(int j=i;j<n;j++)
            {
                p=p*a[j];
                ans=max(ans,p);
            }
        }
        if(ans>=0)printf("Case #%d: The maximum product is %lld.\n",tot++,ans);
        else printf("Case #%d: The maximum product is 0.\n",tot++);
        cout<<endl;
    }
    return 0;
}

 

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值