/
it_100_029.cpp
71 lines (60 loc) · 1.02 KB
/
it_100_029.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
#include <iostream>
bool is_even(int num)
{
return (num & 1) == 0;
}
void reorder_ary_with_odd_even(int ary[], int n, bool(*fun)(int))
{
if(ary == NULL || n == 0) return;
int *front = ary;
int *tail = ary + n -1;
while(front < tail)
{
if(!fun(*front))
{
front++;
continue;
}
if(fun(*tail))
{
tail--;
continue;
}
if(front < tail)
{
int temp = *front;
*front = *tail;
*tail = temp;
}
}
}
void print_ary(int ary[], int n)
{
for(int i = 0 ; i < n; i++)
std::cout<<ary[i]<<" ";
std::cout<<std::endl;
}
void reorder_ary_with_odd_even_2(int ary[], int n, bool(*fun)(int))
{
int end_odd = -1;
int front = 0;
for(int i =0; i < n; i++)
{
if(!fun(ary[front]))
{
end_odd++;
int temp = ary[front];
ary[front] = ary[end_odd];
ary[end_odd] = temp;
}
front++;
}
}
int main()
{
int ary[] ={1,2,3,4,5,6,7,8,9,10};
print_ary(ary, 10);
reorder_ary_with_odd_even_2(ary, 10, is_even);
print_ary(ary, 10);
return 0;
}