找出平面上距离最近的两个点
2015-09-27 10:12
274 查看
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <cmath> #include <string> #include <iostream> #include <vector> #include <algorithm> using namespace std; struct point { point(double x, double y) { x_ = x; y_ = y; } double x_; double y_; }; int main(int argc, char* argv[]) { int n; scanf("%d", &n); vector<point> pvec; for(int i = 0; i < n; ++i) { double x; double y; scanf("%lf%lf", &x, &y); point p(x, y); pvec.push_back(p); } int mlen = 0; int start = 0; int end = 0; for(int i = 0; i < pvec.size(); ++i) { for(int j = i + 1; j < pvec.size(); ++j) { double d2 = pow(pvec[i].x_ - pvec[j].x_, 2) + pow(pvec[i].y_ - pvec[j].y_, 2); if(mlen == 0) { mlen = d2; }else if(mlen > d2) { mlen = d2; start = i; end = j; }else {} } } if(start < end) { printf("closest point: %d,%d\n", start, end); }else { printf("closest point: %d,%d\n", end, start); } return 0; }
相关文章推荐
- 没有数据分析大数据就什么也不是
- 第一章 安装CentOS 7系统
- JS实现仿新浪黄色经典滑动门效果代码
- 回溯---数组序列中所有和为S的组合
- 动态规划——子序列
- *LeetCode-Binary Tree Right Side View
- 工作上一个改进:一个输入序列的稳定输出
- axure7.0注册码Licensee序列号
- Verticle in Vert.x
- 信息安全系统设计基础第二周学习总结
- 多按键监听事件
- Eclipse提交svn错误svn E210003 connection refused by the server
- NSURLConnection 异步请求、同步请求
- 【特征工程系列2】如何获得训练数据的标签?
- 回溯——N皇后
- JFreeChart(二.柱状图.2)
- 面对对象思想6
- 【LeetCode】3Sum 解决报告
- java虚拟机参数
- yii2的rules()函数