UVa 11059 最大乘积

https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2000

题意:找出乘积最大的连续子序列。

思路:这道题目我挺无语的,一直超时,也不知道是哪里出了问题,反正最后试来试去终于有种办法成功了。不过好像这道题目不需要考虑输出0的情况吧。

 1 #include<iostream>
 2 using namespace std;
 3
 4 int main()
 5 {
 6     int ans[20];
 7     int n;
 8     int kase = 0;
 9     while (cin>>n)
10     {
11         kase++;
12         for (int i = 0; i < n; i++)
13             cin >> ans[i];
14         long long max = 0;
15         long long sum;
16         for (int i = 0; i < n; i++)
17         {
18             sum = 1;
19             for (int j = i; j < n; j++)
20             {
21                 sum *= ans[j];
22                 if (sum>max)  max = sum;
23             }
24         }
25         cout << "Case #" << kase << ": The maximum product is " << max << ".\n\n";
26     }
27     return 0;
28 }
时间: 2024-08-24 03:17:19

UVa 11059 最大乘积的相关文章

最大乘积 Uva 11059

输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列.如果这个最大的乘积不是正数,应输出0(表示 无解). 1<=n<=18,-10<=si<=10. 样例输入: 3 2 4 -3 5 2 5 -1 2 -1 样例输出: 8 20 思路: 枚举第一个和最后一个 #include<iostream> #include<cstdio> using namespace std; int main() { int n,m,i,j; int a[20],cse

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

Uva 11059

注意long long  long long  longlong !!!!!!   还有 printf的时候 明明longlong型的答案 用了%d  WA了也看不出,这个细节要注意!!! 1 #include <cstdio> 2 int a[20]; 3 int main() 4 { 5 int n,p=1;long long ans,sum; 6 while(~scanf("%d",&n)) 7 { 8 for(int i=0;i<n;i++) 9 sc

UVa 11059 Maximum Product(简单枚举7.1)使用longlong,输出格式%lld

这题数据最大18位,应该用Long long 粗心在几个地方(函数返回值,中间比较值max pro)用了Int,提交了好几次 #include<stdio.h> long long func(int *a,int head,int rear){//一开始用了int long long pro=a[head];//注意这里初值不能赋值为1 2\n 0 0这个通不过 if(head==rear) return a[head]; else for(int i=head+1;i<=rear;i+

Uva11059

Maximum Product UVA - 11059 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 ?nd a positive sequence, you should consider 0 as the

uva 11595 - Crossing Streets EXTREME(切割多边形)

题目链接:uva 11595 - Crossing Streets EXTREME 对初始平面进行切割,得到所有平面,然后处理出所有边,有公共边的两个平面之间可以到达,对于城市的权值可以加到点上,进出各加一次即可. #include <cstdio> #include <cstring> #include <cmath> #include <queue> #include <vector> #include <complex> #in

uva 1318 - Monster Trap(bfs+暴力)

题目链接:uva 1318 - Monster Trap 每条线段2个点,加上起点终点一个是202个点,暴力判断连点之间是否可达,可达建边.因为线段有厚度考虑,所以将线段延伸一点再处理,这样原本共用一端点的线段变成相交.特殊情况是三点共线,这是判断延伸后的点是否落在其他线段上,如果是就不考虑这个点.最后做一遍bfs. #include <cstdio> #include <cstring> #include <cmath> #include <vector>

UVA 11488 Hyper Prefix Sets (Trie)

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&problem=2483 Hyper Prefix Sets Prefix goodness of a set string islength of longest common prefix*number of strings in the set.For example the prefix goodnes