UVA11059 Maximum Product

问题链接:UVA11059 Maximum Product。基础级练习题,用C语言编写程序。

题意简述:输入n个整数序列,有正有负,求这个序列中最大连续累乘的子序列,其最大的值为多少。如果结果为负数,则输出0。

问题分析:如果整数序列中有0,则用0分段然后分别计算。对于每个分段(可能只有一个分段),其中没有0,如果其中有偶数个负数,则将分段中所有的数相乘就是所求结果。如果分段中有奇数个负数,那么最大的累乘出现在第1个负数的右边开始的子序列或从开始到最后1个负数左边的子序列。

AC的C语言程序如下:

/* UVA11059 Maximum Product */

#include <stdio.h>

int main(void)
{
    int n, val, caseno=0, flag;
    long long ans, max, afternegativemax;

    while(scanf("%d", &n) != EOF) {
        ans = 0;
        max = 1;
        afternegativemax = 1;
        flag = 0;

        while(n--) {
            scanf("%d", &val);

            if(val == 0) {
                max = 1;
                afternegativemax = 1;
                flag = 0;
            } else {
                max *= val;
                if(max > ans)
                    ans = max;

                if(flag) {
                    afternegativemax *= val;
                    if(afternegativemax > ans)
                        ans = afternegativemax;
                }

                if(val < 0)
                    flag = 1;
            }
        }

        printf("Case #%d: The maximum product is %lld.\n\n", ++caseno, ans);
    }

    return 0;
}
时间: 2024-11-08 20:22:23

UVA11059 Maximum Product的相关文章

UVa11059 Maximum Product (Two Pointers)

链接:http://acm.hust.edu.cn/vjudge/problem/27946分析:Two Pointers搞搞. 1 #include <iostream> 2 #include <algorithm> 3 using namespace std; 4 5 typedef long long LL; 6 7 int n, a[20]; 8 9 LL gao() { 10 LL ans = 0; 11 for (int L = 0; L < n; L++) {

3thweek2。uva11059 maximum product.

题意简概: 输入n个元素组成的序列S,你需要找一个乘积最大的连续子序列.如果这个最大的乘积不是正数,应输出0,表示无解.1<=n<=18,-10<=Si<=10. Sample Input32 4 -352 5 -1 2 -1Sample OutputCase #1: The maximum product is 8.Case #2: The maximum product is 20. 简单分析: 连续子序列有两个要素:起点和终点.因此只要枚举起点和终点即可.由于每个元素的绝对值

Maximum Product of Word Lengths

Maximum Product of Word Lengths Total Accepted: 750 Total Submissions: 2060 Difficulty: Medium Given a string array words, find the maximum value of length(word[i]) * length(word[j]) where the two words do not share common letters. You may assume tha

leetcode_152题——Maximum Product Subarray(动态规划)

Maximum Product Subarray Total Accepted: 33022 Total Submissions: 170218My Submissions Question Solution Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2

【LeetCode】Maximum Product Subarray 求连续子数组使其乘积最大

Add Date 2014-09-23 Maximum Product Subarray Find the contiguous subarray within an array (containing at least one number) which has the largest product. For example, given the array [2,3,-2,4],the contiguous subarray [2,3] has the largest product = 

Leetcode - 628 Maximum Product of Three Numbers

Leetcode - 628 Maximum Product of Three Numbers 628. Maximum Product of Three Numbers Given an integer array, find three numbers whose product is maximum and output the maximum product. Example 1: Input: [1,2,3] Output: 6 Example 2: Input: [1,2,3,4]

LeetCode Maximum Product Subarray(枚举)

LeetCode Maximum Product Subarray Description Given a sequence of integers S = {S1, S2, . . . , Sn}, you should determine what is the value of the maximum positive product involving consecutive terms of S. If you cannot find a positive sequence, you

UVa 11059 Maximum Product

题意:给出n个数组成的序列,求乘积最大的连续子序列 看的紫书,因为n最大为18,每个数最大为10,所以10^18用long long 能够存下, 直接枚举起点和终点找最大值就可以了 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include <cmath> 5 #include<stack> 6 #include<vector> 7 #include&l

UVA 11059 Maximum Product(枚举start+end)

题目大意: 就是说给你n个数字,然后求出连续序列的最大和.如果ans为负数就输出0,要么就输出这个ans. 解题思路: 其实我们只需要枚举起点和终点,然后考虑所有的情况就可以了,有点类似于求解最大连续和的问题. 唯一的不同就是每次都要初始化t = 1, t*=a[j].注意long long.因为最多有18个数字,且每个数字的 最大值为10,那么他们的乘积是不会超过10^18的. 代码: # include<cstdio> # include<iostream> # include