发布于 

Be Positive

Be Positive

CF Contest 542 A

题目来源:codeforces

分析

这题一看其实就是一个水题。我们只需要分别判断正负数的个数即可。正数数量\(>= \frac{n}{2}\)就输出1,负数数量\(>= \frac{n}{2}\)就输出-1,否则输出0

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <iostream>

using namespace std;

int cnt[2];

int main()
{
int n;
cin >> n;

cnt[0] = cnt[1] = 0;
for (int i = 1; i<=n; i++)
{
int x;
cin >> x;
if (x>0)
cnt[0] ++;
else if (x<0)
cnt[1] ++;
}

if (cnt[0] >= (n + 1) / 2)
cout << 1 << endl;
else if (cnt[1] >= (n + 1) / 2)
cout << -1 << endl;
else cout << 0 << endl;
}

本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议,转载请注明出处。

本站由 [@Songer](https://blog.songer.xyz/) 创建,使用 Stellar 作为主题。