-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSource.cpp
More file actions
54 lines (41 loc) · 1.21 KB
/
Copy pathSource.cpp
File metadata and controls
54 lines (41 loc) · 1.21 KB
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
#include <iostream>
#include <cstdbool>
#include <vector>
/// <summary>
/// Return Ture if and only if 'x' is in 'vec'
/// </summary>
/// <param name="vec">array to be search in</param>
/// <param name="x">elemnt to find</param>
/// <returns>true if 'x' is in 'vec', false otherwise</returns>
bool search(std::vector<int> vec, int x)
{
// Test if there elemnts in the array
if (vec.size() == 0)
return false;
// If only one element in the array only one position 'x' can be
if (vec.size() == 1)
return vec[0] == x;
// Hold the result
bool answer = false;
// Iterate recursivly thouw all the permutations
for (unsigned int skip = 0; skip < vec.size(); skip++)
{
// Create new vector - similar to the original
std::vector<int> newVec(vec);
// Remove the elemnt in the 'skip' position
newVec.erase(newVec.begin() + skip);
// Search for 'x' in the sub array
answer = answer || search(newVec, x);
}
return answer;
}
int main(int argc, char** argv)
{
// Create some arbitrary vector
std::vector<int> vec;
for (int i = 0; i < 10; i++)
vec.push_back(i);
// Search for the number 5 in 'vec'
std::cout << search(vec, 5) << std::endl;
return 0;
}