hihoCoder#1237 Farthest Point

#1237 : Farthest Point

时间限制:5000ms
单点时限:1000ms
内存限制:256MB

描述

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 }

 

 

 

posted @ 2016-09-11 21:23  wangxiaobao1114  阅读(277)  评论(0编辑  收藏  举报