Farthest Point(微软2016校招笔试题)

Description
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.

Input
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

Output
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.

Sample Input
1.000 1.000 5.000
Sample Output
6 1


1、数据类型确定了,他题目说的float,但是用真的float精度不够,要用double,现在来看,为什么他题目指的应该只是告诉你是小数。最初就应该选用double,本来运算区别也不是太大。
2、常量相乘如i*1.0,得到的结果应该是double,常量默认double。
3、算法:在x的范围内扫描,根据x确定y的相应范围(勾股定理),我们只需要取其两端值进行比较就可以了(端值远离圆心)。
4、范围的取整:上边界向下取整floor(),下边界向上取整ceil()。并不是简单的+0.5再int就可以向上取整,有可能1.1+0.5int=1。
5、从高值端开始扫描,能够减少对于坐标的比较。

python解题报告
http://hihocoder.com/discuss/question/3573


#include <iostream>
#include <cmath>
using namespace std;
const double eps = 1E-7;
double dis(double x, double y, double tx, double ty)
{
    double dist = sqrt((tx - x) * (tx - x) + (ty - y) * (ty - y));
    return dist;
}
void getResult(double x, double y,double r)
{
    int lx = ceil(x - r), rx = floor(x + r), resx, resy;
    double maxDis = 0, r2 = r*r;
    for (int i = rx;i >= lx;--i)
    {
        double d = sqrt(r2 - (i*1.0 - x)*(i*1.0 - x));
        int j0 =ceil(y - d), j1 =  floor(y + d);
        double dis0 = dis(i,j0,x,y),dis1 = dis(i,j1,x,y);
        if ((dis1 - maxDis)>eps)
        {
            maxDis = dis1;
            resx = i;
            resy = j1;
        }
        if ((dis0 - maxDis)>eps)
        {
            maxDis = dis0;
            resx = i;
            resy = j0;
        }
    }
    cout << resx << " " << resy ;
}
int main()
{
    double x, y, r;
    while (cin >> x >> y >> r)
    {
        getResult(x, y, r);
    }
//  system("pause");
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值