zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ButterKnife的使用以及不能自动生成代码问题的解决
    Android事件传递机制
    Java中四种引用类型
    Swiper
    table合并单元格 colspan(跨列)和rowspan(跨行)
    常用JS图片滚动(无缝、平滑、上下左右滚动)代码大全
    解决firefox、chrome不兼容cursor:hand 设置鼠标为手型的方法
    js 验证表单 js提交验证类
    怎么解决浏览器兼容性问题
    JavaScript作用域链
  • 原文地址:https://www.cnblogs.com/wangxiaobao/p/5862801.html
Copyright © 2011-2022 走看看