Skip to content

Commit 1a63afd

Browse files
committed
add
1 parent e591082 commit 1a63afd

File tree

4 files changed

+138
-0
lines changed

4 files changed

+138
-0
lines changed

10.4

18.3 KB
Binary file not shown.

10.4.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#include <iostream>
2+
using namespace std;
3+
4+
const int INF = ~(1<<31);
5+
6+
void swap(int &a, int &b){
7+
a = a^b;
8+
b = a^b;
9+
a = a^b;
10+
}
11+
int flipsign(int a){
12+
int d = a < 0 ? 1 : -1;
13+
int opa = 0;
14+
while(a != 0){
15+
a += d;
16+
opa += d;
17+
}
18+
return opa;
19+
}
20+
int abs(int a){
21+
if(a < 0) a = flipsign(a);
22+
return a;
23+
}
24+
bool opsign(int a, int b){
25+
return (a>0 && b<0) || (a<0 && b>0);
26+
}
27+
int times(int a, int b){
28+
int aa = abs(a), bb = abs(b);
29+
int res = 0;
30+
if(aa < bb) swap(aa, bb);
31+
for(int i=0; i<bb; ++i, res += aa);
32+
if(opsign(a, b)) res = flipsign(res);
33+
return res;
34+
}
35+
int minuss(int a, int b){
36+
return a + flipsign(b);
37+
}
38+
int divide(int a, int b){
39+
if(b == 0) return INF;
40+
int aa = abs(a), bb = abs(b);
41+
int res = 0;
42+
for(; (aa -= bb)>=0; ++res);
43+
if(opsign(a, b)) res = flipsign(res);
44+
return res;
45+
}
46+
int main(){
47+
int a[] = {
48+
8, 0, -8, -5, 9
49+
};
50+
int b[] = {
51+
3, 5, 3, 0, -3
52+
};
53+
for(int i=0; i<5; ++i){
54+
cout<<times(a[i], b[i])<<" "<<minuss(a[i], b[i])<<" "<<divide(a[i], b[i])<<endl;
55+
}
56+
return 0;
57+
}

10.6

71.4 KB
Binary file not shown.

10.6.cpp

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
#include <iostream>
2+
#include <map>
3+
#include <cmath>
4+
#include <cstdlib>
5+
#include <ctime>
6+
using namespace std;
7+
8+
struct point{
9+
double x, y;
10+
};
11+
class line{
12+
public:
13+
double epsilon, slope, intercept;
14+
bool bslope;
15+
public:
16+
line(){}
17+
line(point p, point q){
18+
epsilon = 0.0001;
19+
if(abs(p.x - q.x) > epsilon){
20+
slope = (p.y-q.y) / (p.x-q.x);
21+
intercept = p.y - slope * p.x;
22+
bslope = true;
23+
}
24+
else{
25+
bslope = false;
26+
intercept = p.x;
27+
}
28+
}
29+
void print(){
30+
cout<<"y = "<<slope<<"x + "<<intercept<<endl;
31+
}
32+
};
33+
34+
bool operator <(const line &l1, const line &l2){
35+
return l1.slope < l2.slope;
36+
}
37+
bool equal(double a, double b){
38+
return abs(a-b) < 0.0001;
39+
}
40+
bool operator ==(const line &l1, const line &l2){
41+
if(l1.bslope == l2.bslope && equal(l1.slope, l2.slope) && equal(l1.intercept, l2.intercept))
42+
return true;
43+
return false;
44+
}
45+
line find_best_line(point *p, int point_num){
46+
line bestline;
47+
bool first = true;
48+
map<line, int> line_count;
49+
for(int i=0; i<point_num; ++i){
50+
for(int j=i+1; j<point_num; ++j){
51+
line l(p[i], p[j]);
52+
if(line_count.find(l)==line_count.end())
53+
line_count[l] = 0;
54+
line_count[l] = line_count[l] + 1;
55+
if(first){
56+
bestline = l;
57+
first = false;
58+
}
59+
else{
60+
if(line_count[l] > line_count[bestline])
61+
bestline = l;
62+
}
63+
}
64+
}
65+
cout<<line_count[bestline]<<endl;
66+
return bestline;
67+
}
68+
int main(){
69+
srand((unsigned)time(0));
70+
int graph_size = 10;
71+
int point_num = 500;
72+
point *p = new point[point_num];
73+
for(int i=0; i<point_num; ++i){
74+
p[i].x = rand()/double(RAND_MAX) * graph_size;
75+
p[i].y = rand()/double(RAND_MAX) * graph_size;
76+
cout<<p[i].x<<" "<<p[i].y<<endl;
77+
}
78+
line l = find_best_line(p, point_num);
79+
l.print();
80+
return 0;
81+
}

0 commit comments

Comments
 (0)