UVa 11059:Maximum Product(水题)
2015-09-04 12:38
537 查看
题目链接:https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=841&page=show_problem&problem=2000
题意:输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。(1≤n≤18,−10≤Si≤10)(1 \le n \le 18, -10 \le S_i \le 10)。(本段摘自《算法竞赛入门经典(第2版)》
分析:水题,直接暴力枚举起点终点即可。
代码:
题意:输入n个元素组成的序列S,你需要找出一个乘积最大的连续子序列。如果这个最大的乘积不是正数,应输出0(表示无解)。(1≤n≤18,−10≤Si≤10)(1 \le n \le 18, -10 \le S_i \le 10)。(本段摘自《算法竞赛入门经典(第2版)》
分析:水题,直接暴力枚举起点终点即可。
代码:
#include <fstream> #include <iostream> #include <cstring> #include <algorithm> #include <stack> #include <sstream> #include <string> #include <map> #include <cmath> #include <queue> #include <vector> #include <set> #include <string> #include <vector> using namespace std; const int maxn = 2000 + 5; int n, C; long long ans, tmp; long long a[20]; int main() { while (~scanf("%d", &n)) { ans = 0; for (int i = 0; i < n; ++i) scanf("%lld", &a[i]); for (int i = 0; i < n; ++i) for (int j = i; j < n; ++j) { tmp = 1; for (int k = i; k <= j; ++k) tmp *= a[k]; ans = max(ans, tmp); } printf("Case #%d: The maximum product is %lld.\n\n", ++C, ans); } return 0; }
相关文章推荐
- 如何在Ubuntu中安装JDK
- 更好的自动ssh登录,用公共key实现无密码ssh
- SQLite/嵌入式数据库
- 类内数组声明,“类外”指定大小
- mac默认截图、截图代码
- NSDateFormatter
- C#写的客户端连接 php的服务器端的小例子
- html name id, 与服务器交互必须有name
- 引用类中的enum
- enum可以做索引
- vlan 以及 Linux实现的IEEE 802.1Q VLAN
- gtk+blade+anjuta 的简单实例
- 文件比较,文件夹比较-- vimdiff,beyond compare, compare suite, WinMerge,Kdiff3
- linux 终端控制-- 多彩输出 格式排版
- linux 怎么使用一个c/c++库
- 创作gtk源码级vim帮助文档 tags
- NetBSD是个开源到源码的系统
- 分离整数的各个数位
- Tornado Web服务器
- 文件比较,文件夹比较-- vimdiff,beyond compare, compare suite, WinMerge,Kdiff3