-
Notifications
You must be signed in to change notification settings - Fork 6
/
14_ReorderArray.cpp
65 lines (55 loc) · 944 Bytes
/
14_ReorderArray.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
#include <iostream>
#include <vector>
using namespace std;
bool isOdd(const int& n)
{
return (n & 0x1) == 1;
}
bool isNeg(const int& n)
{
return n < 0;
}
bool isDiviableByThree(const int& n)
{
return n%3 == 0;
}
template <typename T>
void Reorder(vector<T> &arr, bool (*func)(const T&))
{
if (arr.empty())
return;
typename vector<T>::iterator iterbeg = arr.begin(), iterend = arr.end() - 1;
T tmp;
while (iterbeg < iterend)
{
while (iterbeg < iterend && func(*iterbeg))
iterbeg ++;
while (iterbeg < iterend && !func(*iterend))
iterend --;
if (iterbeg < iterend)
{
tmp = *iterbeg;
*iterbeg = *iterend;
*iterend = tmp;
}
}
return;
}
int main()
{
int n, tmp;
vector<int> arr;
cin >> n;
int i = n;
while (n --)
{
cin >> tmp;
arr.push_back(tmp);
}
Reorder(arr, isOdd);
Reorder(arr, isDiviableByThree);
for (int j = 0; j < i; j ++)
cout << arr[j] << " ";
cout<<endl;
return 0;
}