-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy path29_MoreThanHalfNumber.cpp
155 lines (132 loc) · 2.33 KB
/
29_MoreThanHalfNumber.cpp
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <vector>
using namespace std;
#define MAX 100000
void Swap(unsigned long *a, unsigned long *b)
{
unsigned long tmp;
tmp = *a;
*a = *b;
*b = tmp;
}
int Rand(int a, int b)
{
return (int)(a + rand() % (b - a + 1));
}
int QuickSortPartition1(unsigned long *arr, int left, int right)
{
if (arr == NULL || left < 0 || right < 0 || left > right)
return 0;
int rand_num = Rand(left, right);
Swap(&arr[rand_num], &arr[left]);
unsigned long ref = arr[left];
int i, j;
i = left;
j = right;
while (i < j)
{
while (i < j && arr[j] >= ref)
j--;
if (i < j)
{
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] <= ref)
i++;
if (i < j)
{
arr[j] = arr[i];
j--;
}
}
arr[i] = ref;
return i;
}
bool CheckMoreThanHalf(unsigned long *numbers, int length, unsigned long result)
{
int count = 0;
for (int i = 0;i < length; i++)
{
if (numbers[i] == result)
count ++;
}
if (count * 2 <= length)
return false;
else
return true;
}
int MoreThanHalfNum1(unsigned long *numbers, int length)
{
if (numbers == NULL || length <= 0)
{
printf("-1\n");
return 0;
}
int middle = length >> 1;
int left = 0, right = length - 1, index;
index = QuickSortPartition1(numbers, left, right);
while (index != middle)
{
if (index < middle)
{
left = index + 1;
index = QuickSortPartition1(numbers, index + 1, right);
}
else if(index > middle)
{
right = index - 1;
index = QuickSortPartition1(numbers, left, right);
}
}
unsigned long result = numbers[middle];
int count = 0;
for (int i = 0;i < length; i++)
{
if (numbers[i] == result)
count ++;
}
if (count * 2 <= length)
printf("-1\n");
else
printf("%lu\n", result);
return 0;
}
int MoreThanHalfNum2(unsigned long *numbers, int length)
{
int i;
int count = 1;
unsigned long num = numbers[0];
for (i = 0; i < length; i++)
{
if (count == 0)
{
count = 1;
num = numbers[i];
}
else if (numbers[i] != num)
count --;
else
count ++;
}
if (!CheckMoreThanHalf(numbers, length, num))
printf("-1\n");
else
printf("%lu\n", num);
return 0;
}
int main(void)
{
int n;
while (scanf("%d", &n) != EOF)
{
unsigned long numbers[MAX];
for (int i = 0; i < n; i++)
scanf("%lu", &numbers[i]);
MoreThanHalfNum1(numbers, n);
}
return 0;
}