Farthest Point(数学)

在给定圆的条件下,通过计算x轴边界并确定每个横坐标对应的纵坐标范围,找到最远的点。算法涉及ceil和floor函数以及平方根计算。

题目1 : 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.


样例输入
1.000 1.000 5.000
样例输出
6 1

这题题意很简单,我们对于给定的一个圆,坐标为(x,y)半径为r,可以求出满足点的x轴边界,即ceil(x-r),floor(x+r)(ceil表示

取向下取整,floor表示向上取整)。遍历ceil(x-r)到floor(x+r),对于给定的横坐标dx,它的纵坐标为floor(y+sqrt(r*r-(x-dx)*(x-dx))),

ceil(y-sqrt(r*r-(x-dx)*(x-dx)))。然后就可以取最优的那个了。

#include<iostream>
#include<cstdio>
#include<string.h>
#include<string>
#include<algorithm>
#include<vector>
#include<math.h>
using namespace std;
struct Node
{
	int x,y;
	double dis;
}N[200005*2];
bool cmp(Node a,Node b)
{
	if(a.dis!=b.dis)
	{
		return a.dis>b.dis;
	}
	else if(a.x!=b.x)
	{
		return a.x>b.x;
	}
	else
	{
		return a.y>b.y;
	}
}
int main()
{
	double x,y,rr;
	while(~scanf("%lf%lf%lf",&x,&y,&rr))
	{
		double left=ceil(x-rr);
		double right=floor(x+rr);
		int nnn=0;
		for(int i=left;i<=right;i++)
		{
			double xx;
			if(i<x)
				xx=x-i;
			else
				xx=i-x;
			double yy=sqrt(rr*rr-xx*xx);
			N[nnn++].x=i;
			N[nnn-1].y=floor(yy+y);
			
			double t1,t2;
			t1=x-N[nnn-1].x;
			t2=y-N[nnn-1].y;
			N[nnn-1].dis=sqrt(t1*t1+t2*t2);
			N[nnn++].x=i;
			N[nnn-1].y=ceil(y-yy);
			t1=x-N[nnn-1].x;
			t2=y-N[nnn-1].y;
			N[nnn-1].dis=sqrt(t1*t1+t2*t2);
		}
		sort(N,N+nnn,cmp);
		cout<<N[0].x<<" "<<N[0].y<<endl;
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值