【hihocoder】1237 : Farthest Point 微软2016校招在线笔试题
2024-08-29 01:14:57
题目:给定一个圆,要你求出一个在里面或者在边上的整数点,使得这个点到原点的距离最大,如果有多个相同,输出x最大,再输出y最大。
思路:对于一个圆,里面整点个数的x是能确定的。你找到x的上下界就可以了。就是mix = ceil(x0-r)//因为是小的值,所以要向上取整。mxx=floor(x0+r)//大的值要向下取整
对于y。我们也能用欧股定理确定。y也是有一个范围。但是并不是所有y都要枚举的。明显。y的值是离圆心越远越好。所以对于每一个x而言只需要枚举最远的两个y值
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
#define inf (0x3f3f3f3f)
typedef long long int LL; #include <iostream>
#include <sstream>
#include <vector>
#include <set>
#include <map>
#include <queue>
#include <string>
double xo,yo,r;
double dist(double xx1,double yy1,double xx2,double yy2)
{
return sqrt((xx1-xx2)*(xx1-xx2) + (yy1-yy2)*(yy1-yy2));
}
double eps = 1e-;
bool big (double a,double b)
{
return (a-b)>eps;
}
bool same (double a,double b)
{
return fabs(a-b)<eps;
}
void work ()
{
int mix = (int)ceil(xo-r);
int mxx = (int)floor(xo+r);
double mxdis=-;
int ansx,ansy;
for (int i=mix;i<=mxx;++i)
{
int yy = (int)floor((yo+sqrt(r*r-(i-xo)*(i-xo))));
double tdis = dist(i,yy,xo,yo);
if (big(tdis,mxdis))
{
mxdis = tdis; ansx = i; ansy = yy;
}
else if (same(tdis,mxdis)&&i>ansx)
{
mxdis = tdis; ansx = i; ansy = yy;
}
else if (same(tdis,mxdis)&&i==ansx&&yy>=ansy)
{
mxdis = tdis; ansx = i; ansy = yy;
} yy = (int)ceil(yo-sqrt(r*r-(i-xo)*(i-xo)));
tdis=dist(i,yy,xo,yo);
if (big(tdis,mxdis))
{
mxdis = tdis; ansx = i; ansy = yy;
}
else if (same(tdis,mxdis)&&i>ansx)
{
mxdis = tdis; ansx = i; ansy = yy;
}
else if (same(tdis,mxdis)&&i==ansx&&yy>=ansy)
{
mxdis = tdis; ansx = i; ansy = yy;
}
}
printf ("%d %d\n",ansx,ansy);
return ;
}
int main()
{
#ifdef local
freopen("data.txt","r",stdin);
#endif
while(~scanf("%lf%lf%lf",&xo,&yo,&r)) work();
return ;
}
最新文章
- 史上最全的maven的pom.xml文件详解
- 提交留言HTML模板代码
- C#中String.Empty,“”,NULL的区别
- Java基础之集合框架——使用堆栈Stack<;>;对象模拟发牌(TryDeal)
- [iOS基础控件 - 6.10.5] UIApplication
- 一个方便的shell命令,查看软件安装目录
- linux线程之pthread_join
- [HNOI2013]游走 期望+高斯消元
- redis存取对象
- 微信小程序初体验,入门练手项目--通讯录,部署上线(二)
- day5_函数返回值
- VS2013中Python学习笔记[环境搭建]
- JAVA 项目中使用 H2 数据库
- 【Mysql】Mysql修改Root密码
- 作业:WordCount--实现字符数,单词数,行数的统计
- 发现微信支付bug
- 李洪强漫谈iOS开发[C语言-053]-小结
- [AGC026B]rng_10s
- ARM串口控制终端命令
- Verilog HDL设计规范及经验谈(转载)