forked from Hawstein/cracking-the-coding-interview
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
#include <iostream> | ||
using namespace std; | ||
|
||
const int INF = ~(1<<31); | ||
|
||
void swap(int &a, int &b){ | ||
a = a^b; | ||
b = a^b; | ||
a = a^b; | ||
} | ||
int flipsign(int a){ | ||
int d = a < 0 ? 1 : -1; | ||
int opa = 0; | ||
while(a != 0){ | ||
a += d; | ||
opa += d; | ||
} | ||
return opa; | ||
} | ||
int abs(int a){ | ||
if(a < 0) a = flipsign(a); | ||
return a; | ||
} | ||
bool opsign(int a, int b){ | ||
return (a>0 && b<0) || (a<0 && b>0); | ||
} | ||
int times(int a, int b){ | ||
int aa = abs(a), bb = abs(b); | ||
int res = 0; | ||
if(aa < bb) swap(aa, bb); | ||
for(int i=0; i<bb; ++i, res += aa); | ||
if(opsign(a, b)) res = flipsign(res); | ||
return res; | ||
} | ||
int minuss(int a, int b){ | ||
return a + flipsign(b); | ||
} | ||
int divide(int a, int b){ | ||
if(b == 0) return INF; | ||
int aa = abs(a), bb = abs(b); | ||
int res = 0; | ||
for(; (aa -= bb)>=0; ++res); | ||
if(opsign(a, b)) res = flipsign(res); | ||
return res; | ||
} | ||
int main(){ | ||
int a[] = { | ||
8, 0, -8, -5, 9 | ||
}; | ||
int b[] = { | ||
3, 5, 3, 0, -3 | ||
}; | ||
for(int i=0; i<5; ++i){ | ||
cout<<times(a[i], b[i])<<" "<<minuss(a[i], b[i])<<" "<<divide(a[i], b[i])<<endl; | ||
} | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#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; | ||
} | ||
} | ||
void print(){ | ||
cout<<"y = "<<slope<<"x + "<<intercept<<endl; | ||
} | ||
}; | ||
|
||
bool operator <(const line &l1, const line &l2){ | ||
return l1.slope < l2.slope; | ||
} | ||
bool equal(double a, double b){ | ||
return abs(a-b) < 0.0001; | ||
} | ||
bool operator ==(const line &l1, const line &l2){ | ||
if(l1.bslope == l2.bslope && equal(l1.slope, l2.slope) && equal(l1.intercept, l2.intercept)) | ||
return true; | ||
return false; | ||
} | ||
line find_best_line(point *p, int point_num){ | ||
line bestline; | ||
bool first = true; | ||
map<line, int> line_count; | ||
for(int i=0; i<point_num; ++i){ | ||
for(int j=i+1; j<point_num; ++j){ | ||
line l(p[i], p[j]); | ||
if(line_count.find(l)==line_count.end()) | ||
line_count[l] = 0; | ||
line_count[l] = line_count[l] + 1; | ||
if(first){ | ||
bestline = l; | ||
first = false; | ||
} | ||
else{ | ||
if(line_count[l] > line_count[bestline]) | ||
bestline = l; | ||
} | ||
} | ||
} | ||
cout<<line_count[bestline]<<endl; | ||
return bestline; | ||
} | ||
int main(){ | ||
srand((unsigned)time(0)); | ||
int graph_size = 10; | ||
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; | ||
} |