3thweek2。uva11059 maximum product.

题意简概:

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

Sample Input
3
2 4 -3
5
2 5 -1 2 -1
Sample Output
Case #1: The maximum product is 8.
Case #2: The maximum product is 20.

简单分析:

连续子序列有两个要素:起点和终点。因此只要枚举起点和终点即可。由于每个元素的绝对值不超过10且不超18个元素,最大可能的乘积不会超过10^18,用long long 型存储。

代码如下:

 1 #include<iostream>
 2 #include<cstdio>
 3 using namespace std;
 4 int a[18];
 5
 6 int main()
 7 {
 8     int n,x=0;
 9     while(scanf("%d",&n)!=EOF  && n>=1 && n<=18)
10     {
11         int i,j;
12         int maxn=0;
13         for(i=0;i<n;i++)
14             scanf("%d",&a[i]);
15
16         for(i=0;i<n;i++)
17         {
18         int    k=1;            //注意k的位置。每次子序列起点换了时都要重新赋值为1
19             for(j=i;j<n;j++)
20             {
21                 k*=a[j];
22               if(k>maxn)
23                   maxn=k;      //maxn一直保存当前最大值。
24             }
25         }
26         x++;
27       printf("Case #%d: The maximum product is %d.\n\n",x,maxn);
28
29     }
30
31       return 0;
32
33 }

尤其注意输出答案,例如Case的首字母大写,单词间的空格。

时间: 2024-10-03 13:06:56

3thweek2。uva11059 maximum product.的相关文章

UVA11059 Maximum Product

问题链接:UVA11059 Maximum Product.基础级练习题,用C语言编写程序. 题意简述:输入n个整数序列,有正有负,求这个序列中最大连续累乘的子序列,其最大的值为多少.如果结果为负数,则输出0. 问题分析:如果整数序列中有0,则用0分段然后分别计算.对于每个分段(可能只有一个分段),其中没有0,如果其中有偶数个负数,则将分段中所有的数相乘就是所求结果.如果分段中有奇数个负数,那么最大的累乘出现在第1个负数的右边开始的子序列或从开始到最后1个负数左边的子序列. AC的C语言程序如下

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++) {

【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(枚举start+end)

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

LeetCode:Maximum Product Subarray

题目: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 = 6. 这道题属于动态规划的题型,

[Array]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] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen

[LeetCode] 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] Output: 24 Note: The length of the given array will be in range [3,104] and all elemen