您的位置:首页 > 其它

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

2015-10-14 19:35 337 查看
注意以圆点靠左,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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: