离圆心最远的整数点(微软笔试题)
来源:互联网 发布:linux yum命令有什么用 编辑:程序博客网 时间:2024/05/01 17:58
题目链接:http://hihocoder.com/contest/mstest2015sept2/problem/1
题目:
题目1 : Farthest Point
- 样例输入
1.000 1.000 5.000
- 样例输出
6 1
描述
Given a circle on a two-dimentional plane.
Output the integral point in or on the boundary of the circle which has the largest distance from the center.
输入
One line with three floats which are all accurate to three decimal places, indicating the coordinates of the center x, y and the radius r.
For 80% of the data: |x|,|y|<=1000, 1<=r<=1000
For 100% of the data: |x|,|y|<=100000, 1<=r<=100000
输出
One line with two integers separated by one space, indicating the answer.
If there are multiple answers, print the one with the largest x-coordinate.
If there are still multiple answers, print the one with the largest y-coordinate.
分析:
这一题是微软的一道笔试题,笔试题中微软的笔试题是最经典也是难度选择很恰当(有难度但不是变态的那种难的)的,先给个好评。
这一条道题是给定圆心坐标和半径长度,要你求出离圆心最远的整数点,输出其中x最大的点,不唯一则输出y最大的点。
要找出圆内距离圆心最大的点,并且要是整数点,比较先想到的就是对于区间[x - r, x + r]和[y - r, y +r]上的点进行遍历,找出其中在圆上或圆内距离圆心最大的坐标点。
但是,很明显,对于r很大的情况,以上情况会超时。
其实,距离圆心最远的点,肯定是在圆周的周围,我们只要遍历圆周周围(并且不在园外)的点即可,没有必要把圆内部的点都遍历。
那么,我们可以先对x轴遍历,从[x + r, x + r]内的每个整数点x遍历,然后找出当前x点对应的非圆外的y最大值和最小值,进行判断是否是最远点。
如下图:
这样,我们遍历的点数最多也就是2*r*2=4r个。遍历的范围相当于图中的灰色圆环区域。
接下来的问题就是怎么取到符合范围的整数点的问题,在圆内的点,不论是对于x轴还是y轴,
如果是取其max,那么都需要向下靠拢,即如果x + r是1.1,那么我们的x_max只能取1,如果算出来的y是-1.1,那么我们的y_max只能取-2,
而取min则要向上靠拢,即如果x - r是1.1,那么我们的x_min只能取2,如果算出来的y是-1.1,那么我们的y_min只能取-1,
而普通的int()是无法做到这一点,这里我们需要一个地板和天花板函数floor和ceil,具体请看下面表格:
#include <iostream>#include <cmath>using namespace std;int main(){ cout << "int (1.1) = " << int(1.1) << "\t";//int是向0靠 cout << "int (-1.1) = " << int(-1.1) << endl; cout << "floor (1.1) = " << floor(1.1) << "\t";//floor是下靠 cout << "floor (-1.1) = " << floor(-1.1) << endl; cout << "ceil (1.1) = " << ceil(1.1) << "\t";//ceil是想上靠 cout << "ceil (-1.1) = " << ceil(-1.1) << endl; return 0;}结果如下:
AC代码:
#include <iostream>#include <string>#include <cmath>using namespace std;int main(){double x, y, r;cin >> x >> y >> r;double distance = 0;int result_x, result_y;for (double i = floor(x + r); i >= ceil(x - r); --i){//*point:floor,ceilint y_up = floor(y + sqrt(r*r - (x - i)*(x - i)));//确定x对应的y的上限int y_down = ceil(y - sqrt(r*r - (x - i)*(x - i)));//确定y的下限if (y_up < y_down) swap(y_up, y_down);//当差距很小的时候,会有up < down的情况double distance_up = (i - x)*(i - x) + (y_up - y)*(y_up - y);double distance_down = (i - x)*(i - x) + (y_down - y)*(y_down - y);if (distance_up>distance && distance_up <= r*r || distance_up == distance && (i > result_x || (i == result_x && y_up > result_y))){//最远点&&在圆内/上,或者是x或者y更优distance = distance_up;result_x = i;result_y = y_up;}if (distance_down>distance && distance_down <= r*r || distance_down == distance && (i > result_x || (i == result_x && y_down > result_y))){//最远点&&在圆内/上,或者是x或者y更优distance = distance_down;result_x = i;result_y = y_down;}}cout << result_x << " " << result_y << endl;return 0;}
——Apie陈小旭
- 离圆心最远的整数点(微软笔试题)
- 找优雅点(原点在圆心的圆上的整数点)
- HDU -2196 Computer (找出所有点的最远点,着最远点对扩展)
- 半平面的交,二分法(离海最远的点,LA 3890)
- 例题4.10 离海最远的点 LA3890
- 微软笔试题 给一个包含10^7个整数的大文件排序
- 圆心与圆周围点的位置
- 微软的一个笔试题
- poj2187(最远点的距离的平方)
- 寻找平面中距离最远的点
- 51NOD1766 树上的最远点对
- [51nod1766]树上的最远点对
- POJ 2187 平面上的最远点
- 点集中的最近点 最远点对
- 微软笔试题(苏州)
- hdu 5017 模拟退火/三分求椭圆上离圆心最近的点的距离
- 求内切圆的圆心和半径(已知三个点的坐标)
- 求内切圆的圆心和半径(已知三个点的坐标)
- 5个典型的JavaScript面试题
- 微软公司等数据结构+算法面试100题(第1-100题)全部出炉
- visual studio中 用ASP.NET实现 增删改功能实现常见异常及处理
- 自定义控件-PickerView
- leetcode之Factorial Trailing Zeroes
- 离圆心最远的整数点(微软笔试题)
- strcpy与memcpy的区别
- 微软等数据结构+算法面试100题全部答案集锦
- android:theme和app:popupTheme的作用,以及在android 3.0以下不起作用问题的解决
- PS混合模式
- C++模板
- JavaScript Array 对象splice() 方法
- TCP与UDP的区别
- getFragmentManager和getChildFragmentManager使用不当导致viewpager不加载fragment