【hihocoder】1237 : Farthest Point ->微软2016校招在线笔试题

注意以圆点靠左,x的坐标轴 需要用ceil,向前进位

以圆点靠右,x的坐标要用floor

以圆点靠上,y的坐标要用floor

以圆点靠下,y的坐标要用ceil

同时由于要输出x最大,x弱相等,y最大,所以从x最大开始遍历,避免大量比较

/**
 *  @author         johnsondu
 *  @time           19:30 14th Oct 2015
 *  @status         Accepted
 *  @strategy       simple calculate using x^2 + y^2 = r^2
 *                      calculating from the biggest x point and y point.
 *  @problem        1237 : Farthest Point
 *  @url            http://hihocoder.com/problemset/problem/1237
 */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <algorithm>
#include <map>
#include <vector>

const double eps = 1e-7;
double x, y, r, rsquare;
int ans_x, ans_y;
double max_ans;

double dist(double x1, double y1, double x2, double y2)
{
    return (x2-x1) * (x2-x1) + (y2-y1) * (y2-y1);
}

void calc()
{
    int right = floor(x + r);
    int left = ceil(x-r);

    max_ans = 0;
    for(int i = right; i >= left; i --) {
        double p = y + sqrt(r * r - (i-x) * (i-x));
        int ty = floor(p);
        double dis = dist(i*1.0, ty*1.0, x, y);
        if(dis > max_ans) {
            ans_x = i;
            ans_y = ty;
            max_ans = dis;
        }
        p = y - sqrt(r * r - (i-x) * (i-x));
        ty = ceil(p);
        dis = dist(i*1.0, ty*1.0, x, y);
        if(dis > max_ans) {
            ans_x = i;
            ans_y = ty;
            max_ans = dis;
        }
    }
    cout << ans_x << " " << ans_y << endl;
}

int main()
{
    cin >> x >> y >> r;
    calc();

    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值