hihoCoder#1237 Farthest Point
Posted wangxiaobao的博客
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了hihoCoder#1237 Farthest Point相关的知识,希望对你有一定的参考价值。
#1237 : Farthest Point
描述
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. (微软16年秋招第一题)
样例输入
1.000 1.000 5.000
样例输出
6 1
分析:
题意就是找到距离圆心最远的整数点,距离相同时优先考虑x大的,x相同时考虑y比较大的。
遍历x的可能取值,从r + cx 到 r - cx,前者向下取整,后者向上取值。
对于每个x只需要考虑圆内最大和最小的y(其他的距离圆心的距离肯定比这两个小),然后计算距离并比较。
注意:声明变量时没注意把double写了int,导致WA了一次。
代码:
1 #include<iostream> 2 #include<cmath> 3 using namespace std; 4 double cx, cy, r; 5 double getD (int x) { 6 double d = sqrt ( (r * r - (x - cx) * (x - cx) ) ); 7 return d; 8 } 9 10 int main() { 11 12 cin >> cx >> cy >> r; 13 int startx = floor(cx + r); 14 int endx = ceil(cx - r); 15 double maxResult = 0; 16 int resultX = 0, resultY = 0; 17 for (int x = startx; x >= endx; --x) { 18 double dy = getD(x); 19 int y = floor(dy + cy); 20 double dis = (x - cx) * (x - cx) + (y - cy) * (y - cy); 21 if ( dis - r * r < 1e-6 && dis - maxResult > 1e-6) { 22 resultX = x; 23 resultY = y; 24 maxResult = dis ; 25 } 26 y = ceil(cy - dy); 27 dis = (x - cx) * (x - cx) + (y - cy) * (y - cy); 28 if ( dis - r * r < 1e-6 && dis - maxResult > 1e-6) { 29 resultX = x; 30 resultY = y; 31 maxResult = dis ; 32 } 33 } 34 cout << resultX << " " << resultY << endl; 35 }
以上是关于hihoCoder#1237 Farthest Point的主要内容,如果未能解决你的问题,请参考以下文章
[Repost]最远点采样(Farthest Point Sampling)介绍
The Farthest Distance in the World (Tagore)