Description [title]
Containing a given n (0 <n ≤ 1000) an array of integers, find the number of which more than half the number of appearances. It is greater than the number in the array and less than 50 -50.
[Enter]
The first row contains an integer n, the size of the array;
The second line contains n integers, respectively, each element of the array, separated by a single space between the adjacent two elements.
[Output]
If such number exists, the output of this number; otherwise output no.
[Sample input]
3 1 2 2
[Sample Output]
2
Code:
#include <the iostream>
#include <cstdio>
#include <the cmath>
#include <CString>
#include <the cstdlib>
#include <algorithm>
the using namespace STD;
int main ()
{
int n-;
CIN n->>;
int A [ n], f [101] = {}; // array 101 according to the defined range of values
for (int I = 0; I <n-; I ++)
{
CIN >> A [I]; // enter the value
f [a [ i] +50] ++; // bucket sort essence, the number corresponding to the bucket number plus one. There is a detail, I added is 50, since the number of each data range between -50 to 50, but the array is not a negative integer number, so I added survive 50
}
for (int I = 0; I <n-; I ++)
{
IF (F [A [I] +50]> n-/ 2) // If this number exceeds half of the output, and ends
{
COUT << A [I];
return 0;
}
}
cout << "no"; // If not, it outputs "nO"
return 0;
}