GT and sequence
2015-10-24 17:35
295 查看
题目:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1489 Accepted Submission(s): 349
Problem Description
You are given a sequence of N integers.
You should choose some numbers(at least one),and make the product of them as big as possible.
It guaranteed that **the absolute value of** any product of the numbers you choose in the initial sequence will not bigger than 263−1.
Input
In the first line there is a number T (test
numbers).
For each test,in the first line there is a number N,and
in the next line there are N numbers.
1≤T≤1000
1≤N≤62
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
Output
For each test case,output the answer.
Sample Input
1
3
1 2 3
Sample Output
6
题意:
在给定的数字中挑选一些数,使得乘积最大。
思路:
只要是正数肯定要选择,在选择偶数个负数,从小到大进行选择。特殊情况:全部数字都为小于等于0的数,那就把序列中最大的数输出,因为题目要求至少选择一个数。
代码:
GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1489 Accepted Submission(s): 349
Problem Description
You are given a sequence of N integers.
You should choose some numbers(at least one),and make the product of them as big as possible.
It guaranteed that **the absolute value of** any product of the numbers you choose in the initial sequence will not bigger than 263−1.
Input
In the first line there is a number T (test
numbers).
For each test,in the first line there is a number N,and
in the next line there are N numbers.
1≤T≤1000
1≤N≤62
You'd better print the enter in the last line when you hack others.
You'd better not print space in the last of each line when you hack others.
Output
For each test case,output the answer.
Sample Input
1
3
1 2 3
Sample Output
6
题意:
在给定的数字中挑选一些数,使得乘积最大。
思路:
只要是正数肯定要选择,在选择偶数个负数,从小到大进行选择。特殊情况:全部数字都为小于等于0的数,那就把序列中最大的数输出,因为题目要求至少选择一个数。
代码:
#include <cstdio> #include <iostream> #include <cstring> #include <cmath> #include <algorithm> using namespace std; int main(int argc, char const *argv[]) { freopen("in.txt", "r", stdin); int T; scanf("%d", &T); while (T--) { int n, nesum = 0, flag = 0, c = 1; __int64 a[70], b[70], pro = 1; scanf("%d", &n); for (int i = 1; i <= n; i++) { scanf("%I64d", &a[i]); if (a[i] < 0) { nesum++; b[nesum] = a[i]; } else if (a[i] > 0) { pro *= a[i]; flag = 1; } else c = a[i]; } sort(a + 1, a + n + 1); sort(b + 1, b + nesum + 1); int t = nesum / 2 * 2; for (int j = 1; j <= t; j++) { pro *= b[j]; flag = 1; } __int64 ans; if (flag) ans = pro; else ans = pro * a ; printf("%I64d\n", ans); } return 0; }
相关文章推荐
- Array Programming Guide
- 电脑引导那些事(2)--UEFI下win8.1咋改win7,咋装双系统,咋不能激活?
- 优化UITableViewCell高度计算的那些事
- echo 1 > /sys/class/gpio/gpio118/value
- UITableViewCell 使用系统的多选
- EasyUI datetimebox 的onchange事件的问题
- EasyUI - Combo组件
- A cycle was detected in the build path of project
- GUID(转载)
- (转载)全球唯一标识GUID
- 全局唯一标识符(GUID)
- easyui 入门
- hdu5057 Argestes and Sequence 分块
- 淘宝tair--分布式 key/value 存储引擎
- UITabelView 介绍
- 从客户端中检测到有潜在危险的 Request.Form 值 --MVC
- APUE学习总结
- NSOperation之 设置UITabView的有沙盒图片缓存——剖析内在原理
- EasyUI - ValidateBox 验证组件
- 【APUE】Chapter10 Signals