hdu(5504)——GT and sequence
来源:互联网 发布:js 数组中添加对象 编辑:程序博客网 时间:2024/05/24 07:16
问题描述
给出N个整数。你要选择至少一个数,使得你选的数的乘积最大。保证任意选一些数相乘的绝对值都不会大于263−1。
输入描述
第一行读入一个数T表示数据组数。对于每组数据:第一行是一个数N,第二行是N个整数。1≤T≤10001≤N≤62hack时建议输出最后一行的行末回车;每一行的结尾不要输出空格。
输出描述
对于每组数据,输出一个数表示最大的乘积。
输入样例
131 2 3
输出样例
6这题挂了好像很多人,于是决定把它好好的整理一下。。。
题意:中文。。
思路:
分类讨论,情况有点多。
1)如果全是0,那么直接输出0
2)如果只有一个数,那么也是直接输出
3)有偶数个负数,那么结果为负数与正数的相乘
4)有奇数个负数,那么我们先全都把负数乘起来t1,并且把正数也都乘起来保存到另外的一个变量中t2,然后如果只有负数和0的话,如果负数含有的个数大于1的话,那么我们才让答案输出t1*t2/res,其中res是最大的那个负数,因为我们要把它变成正的,所以要除掉,否则的话是0.
#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<cmath>using namespace std;#define maxn 70typedef __int64 ll;ll a[maxn],res;int main(){int T;scanf("%d",&T);while(T--){int n;scanf("%d",&n);for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);sort(a+1,a+1+n);if(n==1){printf("%d\n",a[1]);continue;}if(a[1]==0&&a[n]==0){printf("0\n");continue;}int num1=0;//奇数的个数; for(int i=1;i<=n;i++){if(a[i]<0) num1++;}ll t1=1;if(num1%2==0){for(int i=1;i<=n;i++){if(a[i]==0) continue;else t1=t1*a[i];}printf("%I64d\n",t1);}else{//有奇数个负数; int tx=0;bool ff=false;res=0;ll t2=1;int num1=0,num2=0;for(int i=1;i<=n;i++){if(a[i]<0) num1++;else if(a[i]==0) num2++;}for(int i=1;i<=n;i++){if(a[i]<0){t1=t1*a[i];if(a[i]>res||res==0) res=a[i];}else if(a[i]>0){t2=t2*a[i];}}if(t1<0) t1=t1/res;ll ans=0;if(num1+num2==n){ans=0;if(num1>1){ans=t1;}}else {ans=t1*t2;}printf("%I64d\n",ans);}}}/*130 -1 0*/
0 0
- hdu(5504)——GT and sequence
- HDU 5504:GT and sequence
- HDU 5504 GT and sequence
- HDU 5504 GT and sequence
- HDU 5504 GT and sequence 模拟
- HDU 5504 GT and sequence(贪心)
- hdu 5504 GT and sequence(dp)
- GT and sequence 5504GT and sequence
- HDU 5504 GT and sequence(排除陷阱就是正解)——BestCoder Round #60
- 5504 GT and sequence
- hdu 5504 GT and sequence【BestCoder Round #60 】
- hdu 5504 GT and sequence(思维,逻辑,脑洞)
- HDOJ 5504-GT and sequence
- hdu(5505)——GT and numbers
- HDOJ 5504 GT and sequence (乱搞)
- 杭电5504 GT and sequence
- 杭电-5504GT and sequence
- hdu5504 GT and sequence
- java中的8个基本数据类型
- 控件懒加载
- Centos系统备份与恢复教程
- Windows Server 2008 R2 简体中文版 显示中文乱码
- 字符类型 String 怎样转换成 日期类型 Date?
- hdu(5504)——GT and sequence
- jQuery基本选择器
- Android 博客目录整理
- Linux下MySQL卸载和安装图文教程详解
- iptables命令
- 第一行代码笔记10(WebView控件)
- (158)两个字符串是变位词
- 笔记本电脑增加内存条步骤
- asp.net的自定义控件和用户控件的区别