A. Be Positive
A. Be Positive
1 second
256 megabytes
standard input
standard output
You are given an array of nn integers: a1,a2,…,ana1,a2,…,an. Your task is to find some non-zero integer dd (−103≤d≤103−103≤d≤103) such that, after each number in the array is divided by dd, the number of positive numbers that are presented in the array is greater than or equal to half of the array size (i.e., at least ⌈n2⌉⌈n2⌉). Note that those positive numbers do not need to be an integer (e.g., a 2.52.5 counts as a positive number). If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.
Recall that ⌈x⌉⌈x⌉ represents the smallest integer that is not less than xx and that zero (00) is neither positive nor negative.
Input
The first line contains one integer nn (1≤n≤1001≤n≤100) — the number of elements in the array.
The second line contains nn space-separated integers a1,a2,…,ana1,a2,…,an (−103≤ai≤103−103≤ai≤103).
Output
Print one integer dd (−103≤d≤103−103≤d≤103 and d≠0d≠0) that satisfies the given condition. If there are multiple values of dd that satisfy the condition, you may print any of them. In case that there is no such dd, print a single integer 00.
Examples
input
5
10 0 -7 2 6
output
4
input
7
0 0 1 -1 0 0 2
output
0
Note
In the first sample, n=5n=5, so we need at least ⌈52⌉=3⌈52⌉=3 positive numbers after division. If d=4d=4, the array after division is [2.5,0,−1.75,0.5,1.5][2.5,0,−1.75,0.5,1.5], in which there are 33 positive numbers (namely: 2.52.5, 0.50.5, and 1.51.5).
In the second sample, there is no valid dd, so 00 should be printed.
题解:计算出正数和负数的个数,如果正数大于(n+1)/2的话,就是随便输出一个正数,如果负数大于(n+1)/2的话,就随便输出一个负数,否则输出0.
#include<iostream>
#include<cstdio> using namespace std; int main()
{
int n,a[],cnt=,cnt2=;
cin>>n;
for(int i=;i<n;i++)
{
scanf("%d",a+i);
if(a[i]>) //找出正数的个数
cnt++;
else if(a[i]<) //找出负数的个数
cnt2++; }
if(cnt>=(n+)/)
printf("1\n");
else if(cnt2>=(n+)/)
printf("-1\n");
else
printf("0\n");
return ;
}
A. Be Positive的更多相关文章
- [LeetCode] First Missing Positive 首个缺失的正数
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Leetcode First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
- 【leetcode】First Missing Positive
First Missing Positive Given an unsorted integer array, find the first missing positive integer. For ...
- 【leetcode】First Missing Positive(hard) ☆
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- LeetCode - 41. First Missing Positive
41. First Missing Positive Problem's Link ---------------------------------------------------------- ...
- LeetCode题解-----First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example,Given [1,2,0] ...
- hdu 5183. Negative and Positive (哈希表)
Negative and Positive (NP) Time Limit: 3000/1500 MS (Java/Others) Memory Limit: 65536/65536 K (Ja ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- [LintCode] Interleaving Positive and Negative Numbers
Given an array with positive and negative integers. Re-range it to interleaving with positive and ne ...
随机推荐
- c++学习笔记之类和对象(二、构造函数和析构函数)
1.构造函数(Constructor):在C++中,有一种特殊的成员函数,它的名字和类名相同,没有返回值,不需要用户显式调用(用户也不能调用),而是在创建对象时自动执行. 这种特殊的成员函数就是构造函 ...
- 单个html5页面加个密码访问
单个html5页面要实现加个密码才能访问,可以用js来控制.代码加在<head>插入下面代码</head>代码如下: <script languange="Ja ...
- Jconsole与Jmx 分析JVM状况(下) 转
出处: Jconsole与Jmx 分析JVM状况(下) 线程(ThreadMXBean ) 从 Jconsole 画面取得线程画面如下: 左下角列出了所以正在运行的线程.通过点击某个线程,右下脚可以看 ...
- ProGuard 最全混淆规则说明
Input/Output Options 输入输出选项 -include filename 递归引入目录的配置文件 -basedirectory directoryname -injars class ...
- Python-RabbitMQ-fanout(广播模式)
生产者:fanout_publiser.py import pika import sys connection = pika.BlockingConnection(pika.ConnectionPa ...
- MySQL 下载与安装
从MySQL官网下载安装文件,我的电脑是window10 64位的,下载社区版本,选择mysql community server,再下载64位的. 官网链接:https://www.mysql.co ...
- Java高并发程序设计学习笔记(一):并行简介以及重要概念
转自:https://blog.csdn.net/dataiyangu/article/details/86211544#_28 文章目录为什么需要并行?反对意见大势所趋几个重要的概念同步(synch ...
- shell脚本中的数组
以下命令,都是以数组array=("20150417" "20150416" "20150415")为例. 注意bash中只支持一维数组,没 ...
- 第二篇.2、python基础之字符编码
一 了解字符编码的知识储备 一 计算机基础知识 二 文本编辑器存取文件的原理(nodepad++,pycharm,word) #1.打开编辑器就打开了启动了一个进程,是在内存中的,所以,用编辑器编写的 ...
- Centos7.0 三种网络适配器
VMnet0:桥接模式 VMnet1:主机模式 VMnet8:NAT模式 VMware Network Adepter VMnet1:宿主Host用于与 主机模式 虚拟网络进行通信的虚拟网卡 VMwa ...