Skip to content

Commit

Permalink
add
Browse files Browse the repository at this point in the history
  • Loading branch information
Hawstein committed Apr 27, 2013
1 parent 1a63afd commit f3a743e
Show file tree
Hide file tree
Showing 6 changed files with 122 additions and 2 deletions.
Binary file added 10.6.1
Binary file not shown.
78 changes: 78 additions & 0 deletions 10.6.1.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,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;
}
Binary file added 10.7
Binary file not shown.
44 changes: 44 additions & 0 deletions 10.7.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#include <iostream>
#include <queue>
using namespace std;

int mini(int a, int b){
return a < b ? a : b;
}
int mini(int a, int b, int c){
return mini(mini(a, b), c);
}
int get_num(int k){
if(k <= 0) return 0;
int res = 1, cnt = 1;
queue<int> q3, q5, q7;
q3.push(3); q5.push(5); q7.push(7);
for(; cnt<k; ++cnt){
int v3 = q3.front();
int v5 = q5.front();
int v7 = q7.front();
res = mini(v3, v5, v7);
if(res == v7){
q7.pop();
}
else{
if(res == v5){
q5.pop();
}
else{
if(res == v3){
q3.pop();
q3.push(3*res);
}
}
q5.push(5*res);
}
q7.push(7*res);
}
return res;
}
int main(){
for(int i=1; i<20; ++i)
cout<<get_num(i)<<endl;
return 0;
}
Binary file modified 9.3
Binary file not shown.
2 changes: 0 additions & 2 deletions 9.3.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#include <iostream>
using namespace std;

int cnt = 0;
int search(int a[], int low, int high, int x){
while(low <= high){
//cout<<low<<" "<<high<<endl;
Expand Down Expand Up @@ -37,6 +36,5 @@ int main(){
2,2,2,2,2,2,2,2,3,2,2,2,2,2,2,2,2,2,2
};
cout<<search(a, 0, 11, 3)<<endl;
cout<<cnt<<endl;
return 0;
}

0 comments on commit f3a743e

Please sign in to comment.