hdu5504
来源:互联网 发布:淘宝客推广教程全攻略 编辑:程序博客网 时间:2024/06/05 20:27
GT and sequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1321 Accepted Submission(s): 308
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 than263−1 .
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
Input
In the first line there is a number T (test numbers).
For each test,in the first line there is a numberN ,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.
For each test,in the first line there is a number
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
131 2 3
Sample Output
6
一开始看这道题,看成了让选几个数,使得这几个数的乘积是不大于2^63-1的,并且是最大的,再读一遍,才发现是题中保证,任意的数的乘积不大于2^63-1,这样这道题就变得简单多了。但是如果要注意一些特殊情况,比如说,本来如果有0的话是绝对不选的,但是如果只有0的话,就只能选0,如果只有一个负数,剩下的全是0的话,那么也只能选0,如果只有一个负数的话,那么就只能选择负数,剩下的情况中,答案及时,所有数的乘积除以最大的那个负数了。
(话说,《设计模式》中的模板方法和c++中的模板方法是一种东西吗?应该不是吧(错),答:模板模式常用模板方法来实现!!!)
已ac的代码:
#include<stdio.h>#include<string.h>#include<iostream>using namespace std;#define N 70long long int num[N];int main(){ int t; int n; int znum; int fnum; long long int fmax; long long int ans; scanf("%d",&t); while(t--){ ans=1; fnum=0; znum=0; scanf("%d",&n); for(int i=1;i<=n;i++){ scanf("%lld",&num[i]); if(num[i]<0){ if(fnum==0){ fmax=num[i]; } else if(fmax<num[i]){ fmax=num[i]; } fnum++; } if(num[i]!=0){ ans=ans*num[i]; } else{ znum++; } } if(fnum%2){// 这个判断需谨慎考虑。 if(fnum==1){ if(fnum+znum==n&&znum!=0){ ans=0; } else if(fnum!=n&&fnum+znum!=n){ ans=ans/fmax; } } else{ ans=ans/fmax; } } else if(n==znum){ ans=0; } printf("%lld\n",ans); } return 0;}
0 0
- hdu5504
- hdu5504
- hdu5504 GT and sequence
- hdu5504 GT and sequence
- hdu5504 GT and sequence
- hdu5504(水题,细节)
- 我家博宇生日快乐!!!hdu5504
- BestCoder Round #60 HDU5504 GT and sequence
- hdu5504 GT and sequence(BestCoder Round #60 )
- Must known Tips/Tricks in Deep Neural Networks(译文)
- chrome 开发者工具使用技巧
- Hibernate如何提升数据库查询的性能
- 自学考试所取“真经”
- IOS学习 NSAssert和NSAssertion?Handler:断言相关
- hdu5504
- Parallels Desktop中如何使用鼠标和键盘设置
- TCP的建立和终止
- 租用游艇问题
- IOS学习 postNotificationName 消息传递详解
- TCP缓冲区大小及限制
- 8个实现在线浏览PDF文件的实用jQuery插件
- Linux学习笔记-结构化命令2
- easyui 调用ajax时的loading效果