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 ...
随机推荐
- IDEA Git回退到指定历史版本
1.找到要回退的版本号(右击项目--> Git --> Show History -->选中要回退的版本-->Copy Revision Number): 2.打开idea的T ...
- Centos7 更换为网易YUM源
当我们刚刚安装系统的时候 yum 的速度那是真滴慢所以我们就需要一个更加快速的镜像,这时候网易镜像带给我们便捷.下面来一起更换吧! 备份当前的 yum 源 # yum 源在目录 /etc/yum.re ...
- LinkedList实现基于LRU算法的缓存
LinkedList实现基于LRU算法的缓存 2015年08月07日 18:18:45 秦江波 阅读数 2068 文章标签: java算法linkedlist缓存LRU更多 分类专栏: Java ...
- idea-代码格式化快捷键设置(2019.1版)
idea默认格式化快捷键是:Ctrl+Alt+L,有时会因其它软件快捷键的冲突导致失灵. 设置方法如下: 1.File --> Settings... 2. Keymap -> Code ...
- 第二章 单表查询 T-SQL语言基础(2)
单表查询(2) 2.2 谓词和运算符 T-SQL有几种不同的语言元素可以指定逻辑表达式,例如,查询过滤器(WHERE和HAVING),CHECK约束,等等. 在逻辑表达式中可以使用各种谓词(取值为TR ...
- EJS学习(五)之EJS的CommonJs规范版本
EJS的CommonJs规范版本 ejs分为两个版本一个是CommonJs版本,另外一个是AMD规范的版本. 基础:html页面 安装:<script type="text/javas ...
- js获取url(request)中的参数
index.htm?参数1=数值1&参数2=数值2&参数3=数据3&参数4=数值4&...... 静态html文件js读取url参数,根据获取html的参数值控制htm ...
- 忘记oracle的sys用户密码如何修改以及Oracle 11g 默认用户名和密码
忘记除SYS.SYSTEM用户之外的用户的登录密码 CONN SYS/PASS_WORD AS SYSDBA; --用SYS (或SYSTEM)用户登录 ALTER USER user_name ID ...
- 从FBV到CBV三(权限)
丛FBC到CBV三(权限) span::selection, .CodeMirror-line > span > span::selection { background: #d7d4f0 ...
- 如何删掉git版本库master分支的第一个commit
这个操作会将库清空,一般来说在建库开始的时候操作. 适用场景: git init初始化版本库之后,提交第一个点之后发现这个点出问题了,但是此时内心如果有洁癖的话, 你会觉得不完美,很想把这个点干掉重来 ...