summaryrefslogtreecommitdiff
path: root/cpp/ricercaBinaria.cpp
blob: a3e510bf1d18e310d653177f9393a864931c4e5c (plain)
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
#include <iostream>
#include <vector>
#include <string.h>

using namespace std;

class Bar{
	protected:
		int x;
		vector<long long> f;

	public:
		virtual void foobar() = 0;

		auto setX(const int x) -> void{
			f.erase(f.begin(), f.end());

			this->x = x;

			fib();
		};

		auto fib() -> void {
			f.push_back(1);
			f.push_back(1);

			for(int i = 2; i < x; i++)
				f.push_back(f.at(i-1) + f.at(i-2));

		};

		auto getFib() const -> void {
			for(auto i : f)
				cout << i << ' ';

			cout << '\n';
		};

		auto ricerca(long long n) const -> bool {
			vector<long long> f2(f);
			bool s = false;
			int met_div;
			do {
				met_div = f2.size() / 2;

				if(n == f2.at(met_div)) {
					s = true;
					break;
				}else if(n > f2.at(met_div)) {
					f2.erase(f2.begin(), f2.begin()+met_div);
				} else {
					f2.erase(f2.begin()+met_div, f2.end());
				}

				if(f2.size() == 1) {
					if(f.at(0) == n)
						s = true;
					else
						break;
				}

			} while(!s);

			return s;
		};
};

class Foo : public Bar {
	public:
		void foobar() override {
			cout << "FOOBAR!!" << endl;
		}

		Foo(int x) {
			this->x = x;
			fib();
		}

};

int main(int argc, char** argv)
{
	Foo foo(10);
	int sx;
	long long valRic;
	foo.getFib();

	cin >> valRic;

	cout << (foo.ricerca(valRic) ? "Presente" : "Non presente") << endl;

	cin >> sx;
	foo.setX(sx);

	foo.getFib();

	cin >> valRic;
	cout << (foo.ricerca(valRic) ? "Presente" : "Non presente") << endl;

	foo.foobar();

	return 0;
}