UvaUva11059 最大乘积

Uva11059 最大乘积

题目描述:

输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。

1738942-20190817203654381-1931751095.png

思路:

记录起点和终点,两遍循环找到这个最大的值。这里子序列的最小长度为1。

代码:
#include <iostream>
#include <limits.h>
#define LL long long
using namespace std;
const int maxn = 20;
int nums[maxn];

LL solve(int nums[], int i, int j){
    LL res = 1;
    while(i <= j){
        res = res * (LL)nums[i];
        ++i;
    }
    return res;
}

int main(){
    //freopen("uva11059_in.txt", "r", stdin);
    //freopen("uva11059_out.txt", "w", stdout);
    int n; int kase = 0;
    while(cin >> n){
        ++kase;
        LL max = INT_MIN;
        LL res;
        for(int i = 0; i < n; ++i){
            scanf("%d", &nums[i]);
        }
        for(int i = 0; i < n; ++i){
            for(int j = i; j < n; ++j){
                res = solve(nums, i, j);
                if(res > max) {max = res;}  
            }
        }
        if(max < 0) printf("Case #%d: The maximum product is 0.\n", kase);
        else printf("Case #%d: The maximum product is %lld.\n", kase, max);
        printf("\n");
    }
} 

转载于:https://www.cnblogs.com/patrolli/p/11370253.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值