-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathArithematicExpressions.cpp
95 lines (64 loc) · 1.57 KB
/
ArithematicExpressions.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
#include <vector>
#include <iostream>
#include <cmath>
const double Epsilon = 0.00001;
void eval(const std::vector<double>& numbers, std::vector<bool>& used)
{
if (numbers.size() == 1)
{
auto result = numbers.front() + Epsilon;
if (fmod(result, 1) > 10*Epsilon)
return;
int index = int(result + Epsilon);
if (index >= 0 && index < (int)used.size())
used[index] = true;
return;
}
auto next = numbers;
for (size_t i = 0; i < numbers.size(); i++)
for (size_t j = i + 1; j < numbers.size(); j++)
{
double a = numbers[i];
double b = numbers[j];
next = numbers;
next.erase(next.begin() + j);
next.erase(next.begin() + i);
next.push_back(a + b);
eval(next, used);
next.back() = a - b;
eval(next, used);
next.back() = b - a;
eval(next, used);
next.back() = a * b;
eval(next, used);
if (b != 0)
{
next.back() = a / b;
eval(next, used);
}
if (a != 0)
{
next.back() = b / a;
eval(next, used);
}
}
}
unsigned int getSequenceLength(const std::vector<double>& numbers)
{
std::vector<bool> used(1000, false);
eval(numbers, used);
unsigned int result = 0;
while (used[result + 1])
result++;
return result;
}
int main()
{
unsigned int numDigits;
std::cin >> numDigits;
std::vector<double> numbers(numDigits);
for (auto& x : numbers)
std::cin >> x;
std::cout << getSequenceLength(numbers);
return 0;
}