二位平面上,经过点数最多的线
来源:互联网 发布:不想找女朋友 知乎 编辑:程序博客网 时间:2024/03/29 23:06
#include<iostream>#include<vector>#include<math.h>#include<hash_map>using namespace std;class Point{public:double x;double y;Point() :x(0.0), y(0.0){}Point(double _x, double _y) :x(_x), y(_y){}};class Line{private:bool infinite_slope = false;public:double epsilon = 0.0001;double slope;double intercept;Line(){}Line(Point p1, Point p2){if (fabs(p1.x - p2.x) > epsilon){slope = (p1.y - p2.y) / (p1.x - p2.x);intercept = p1.y - slope*p1.x;}else{infinite_slope = true;intercept = p1.x;}}double floorToNearestEpsilon(double d){int r = (int)(d / epsilon);return ((double)r)*epsilon;}bool isEquivalent(double a, double b){ return fabs(a - b) < epsilon; }bool isEquivalent(Line& l){if (isEquivalent(l.slope, slope) && isEquivalent(l.intercept, intercept) && (infinite_slope == l.infinite_slope))return true;return false;}};int countEquivalentLines(vector<Line> lines, Line line){if (lines.size() == 0) return 0;int count = 0;for (int i = 0; i < (int)lines.size(); ++i)if (lines[i].isEquivalent(line))++count;return count;}int countEquivalentLines(hash_map<double, vector<Line>> lineBySlope, Line line){double key = line.floorToNearestEpsilon(line.slope);int count = countEquivalentLines(lineBySlope[key], line) + countEquivalentLines(lineBySlope[key - line.epsilon],line) + countEquivalentLines(lineBySlope[key + line.epsilon],line);return count;}void insertLine(hash_map<double, vector<Line>>& lineBySlope, Line& line){vector<Line> v;double key = line.floorToNearestEpsilon(line.slope);hash_map<double, vector<Line>>::iterator it = lineBySlope.find(key);if (it == lineBySlope.end()){lineBySlope[key] = v;lineBySlope[key].push_back(line);}else{lineBySlope[key].push_back(line);}}Line findBestLine(Point points[],int length){Line bestLine;int bestCount = 0;hash_map<double, vector<Line>> linesBySlope;for (int i = 0; i < length; ++i){for (int j = i + 1; j < length; ++j){Line line(points[i], points[j]);insertLine(linesBySlope, line);int count = countEquivalentLines(linesBySlope, line);if (count > bestCount){bestLine = line;bestCount = count;}}}return bestLine;}int main(){Point p[6] ;p[0] = Point(1, 1);p[1] = Point(2, 3);p[2] = Point(3, 3);p[3] = Point(4, 5);p[4] = Point(6, 10);p[5] = Point(7, 7);Line l = findBestLine(p,sizeof(p)/sizeof(Point));cout << std::fixed << l.intercept << " " << l.slope << endl;cin.get();return 0;}
结果显示正确
0 0
- 二位平面上,经过点数最多的线
- 在二维平面上,有一些点。请找出经过点数最多的那条线
- 9.7数学与概率(四)——在二维平面上,有一些点,请找出经过点数最多的那条线
- java 平面上在一条直线上最多的点数
- 求二维平面上共线最多的点数
- 寻找平面内共线点数最多的直线和点数
- POJ 1118+2606+2780 (求平面上同一条直线的最多点数)
- 程序员面试金典: 9.7数学与概率 7.6在二维平面上,有一些点,请找出经过点最多的那条线
- 【Codeforces Round 374 (Div 2)C】【DAG上的DP】Journey 有向无环图从1到n时刻T内的最多经过点数
- 求一条直线经过一个直角坐标系里m个点最多点的点数
- UVA Lining Up (一条直线上最多的点数)
- 绳子可以覆盖的最多点数
- 给定2D平面上的n个点,找到位于同一直线上的最大点数
- max-points-on-a-line(求最多点数的一条线)
- 三角形上的格点数
- 题目1548:平面上的点:直线穿过最多点(不应该这样AC吗?)
- 平面上n个点,半径为r的圆最多能覆盖几个点
- 求穿过平面上最多点的直线(设计思想)
- VirtualBox虚拟机网络环境解析和搭建-NAT、桥接、Host-Only、Internal、端口映射
- Flask环境搭建
- NodeJS 和 ExtJS
- Nodejs+Extjs+Mongodb开发第一天 Nodejs环境搭建
- 坑
- 二位平面上,经过点数最多的线
- JS在function前面加上"+"的含义
- c++与c#类型对照
- git pull
- 实用linux命令整理
- git fetch
- ul li 下图片文字垂直居中
- javascript系列教程
- Git 冲突处理