首页 > 其他 > 详细

[2016-02-19][UVA][11059][Maximum Product]

时间:2016-02-19 01:37:39      阅读:185      评论:0      收藏:0      [点我收藏+]
[2016-02-19][UVA][11059][Maximum Product]

  • 时间:2016-02-19 00:32:21 星期五
  • 题目编号:UVA 11059
  • 题目大意:
    • 给定n,和长度为n 的序列,求最长连续序列,使得乘积最大,如果乘积为负数,则输出0
  • 分析:
    • 最多18个数字,每个数字最大为10最小为-10,所以可以用longlong 保存
  • 方法:暴力枚举位置:i,j,累乘i~j范围内的数字
  • 解题过程遇到问题:
    • 刚开始枚举j的时候是0~n-1枚举,没考虑到j >= i,会出现j < i的时候,res直接被赋值1,(o(︶︿︶)o 写挫...),j改成i~n-1就可以了



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <vector>
#include <list>
#include <map>
#include <set>
#include <deque>
#include <queue>
#include <stack>
#include <bitset>
#include <algorithm>
#include <functional>
#include <numeric>
#include <utility>
#include <sstream>
#include <iostream>
#include <iomanip>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <cctype>
#include <string>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
typedef long long LL;
#define CLR(x,y) memset((x),(y),sizeof((x)))
#define FOR(x,y,z) for(int (x)=(y);(x)<(z);(x)++)
#define FORD(x,y,z) for(int (x)=(y);(x)>=(z);(x)--)
#define FOR2(x,y,z) for((x)=(y);(x)<(z);(x)++)
#define FORD2(x,y,z) for((x)=(y);(x)>=(z);(x)--)
const int maxn = 18 + 2;
int main(){
        int n,a[maxn],cntcase = 0;
        while(~scanf("%d",&n)){
                FOR(i,0,n)      scanf("%d",&a[i]);
                LL res = 0;
                FOR(i,0,n){
                        FOR(j,i,n){
                                LL tmp = 1;
                                FOR(k,i,j+1)      tmp *= a[k];
                                if(res < tmp)   res = tmp;
                        }                        
                }
                printf("Case #%d: The maximum product is %lld.\n\n",++cntcase,res);        
        }
    return 0;
}



















[2016-02-19][UVA][11059][Maximum Product]

原文:http://www.cnblogs.com/qhy285571052/p/5199679.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!