【hihocoder】1237 : Farthest Point ->微软2016校招在线笔试题
2015-10-14 19:35
337 查看
注意以圆点靠左,x的坐标轴 需要用ceil,向前进位
以圆点靠右,x的坐标要用floor
以圆点靠上,y的坐标要用floor
以圆点靠下,y的坐标要用ceil
同时由于要输出x最大,x弱相等,y最大,所以从x最大开始遍历,避免大量比较
以圆点靠右,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; }
相关文章推荐
- iOS开发-14款状态栏(StatusBar)开源软件
- 笔试题——char能否存中文汉字
- 信息系统项目管理师:论项目的质量管理
- load和initialize的区分
- 1027. Colors in Mars
- 关于与运算和取余之间的关系
- 面试用友的前后;笔试跟面试
- The first blog:不称职的软件工程本科生到码农研究生的转变。
- NIM 博弈游戏
- 1015. Reversible Primes
- poj 2914 全局最小割(stoer_wagner算法)
- iOS开发者必备:四款后端服务工具
- iOS 字符串的UTF8 编码 以及归档反归档
- 信息系统项目管理师:论项目的沟通管理
- android - failed to pull a selection
- 【PAT】1084. Broken Keyboard (20)
- C#学习日记24----事件(event)
- openstack keystone v2 to v3
- wxpay-is-shit
- 学会生活