forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
/
10.6.1.cpp
78 lines (76 loc) · 1.89 KB
/
10.6.1.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
#include <iostream>
#include <map>
#include <cmath>
#include <cstdlib>
#include <ctime>
using namespace std;
struct point{
double x, y;
};
class line{
public:
double epsilon, slope, intercept;
bool bslope;
public:
line(){}
line(point p, point q){
epsilon = 0.0001;
if(abs(p.x - q.x) > epsilon){
slope = (p.y-q.y) / (p.x-q.x);
intercept = p.y - slope * p.x;
bslope = true;
}
else{
bslope = false;
intercept = p.x;
}
}
int hashcode(){
int sl = (int)(slope * 1000);
int in = (int)(intercept * 1000);
return sl*1000 + in;
}
void print(){
cout<<"y = "<<slope<<"x + "<<intercept<<endl;
}
};
line find_best_line(point *p, int point_num){
line bestline;
bool first = true;
map<int, int> mii;
for(int i=0; i<point_num; ++i){
for(int j=i+1; j<point_num; ++j){
line l(p[i], p[j]);
if(mii.find(l.hashcode()) == mii.end()){
mii[l.hashcode()] = 0;
}
mii[l.hashcode()] = mii[l.hashcode()] + 1;
if(first){
bestline = l;
first = false;
}
else{
if(mii[l.hashcode()] > mii[bestline.hashcode()])
bestline = l;
}
}
}
// int a = mii[bestline.hashcode()];
// cout<<mii[bestline.hashcode()]<<endl;
// cout<<(1+sqrt(1+8*a))/2<<endl;
return bestline;
}
int main(){
srand((unsigned)time(0));
int graph_size = 100;
int point_num = 500;
point *p = new point[point_num];
for(int i=0; i<point_num; ++i){
p[i].x = rand()/double(RAND_MAX) * graph_size;
p[i].y = rand()/double(RAND_MAX) * graph_size;
//cout<<p[i].x<<" "<<p[i].y<<endl;
}
line l = find_best_line(p, point_num);
l.print();
return 0;
}