问题:http://hihocoder.com/contest/hiho111/problem/1
详解:http://hihocoder.com/discuss/question/3573
注意点:
浮点类型的比较方式
设定极小量eps,对于两个浮点数a,b
逻辑关系 | 比较方式 |
---|---|
a==b | abs(a-b)<eps |
a != b | abs(a-b)>eps |
a>b | a-b>eps |
a>=b | a-b>-eps |
实现:
#include
#include
using namespace std;
#define MAX 1000000
#define eps 1e-6
double x, y, r;
inline int choose(int y1, int y2, double d)
{
if (y1 >= d && y2 <= d)
return y1 - d >= d - y2 ? y1 : y2;
else if (y1 >= d)
return y1;
else if (y2 <= d)
return y2;
else
return MAX;
}
void solve()
{
double max, rs, d, t;
int yt, m_x, m_y;
cin >> x >> y >> r;
rs = r * r, max = m_x = m_y = -1;
for (int i = floor(x + r); i >= ceil(x - r); --i)
{
t = (i - x) * (i - x);
d = sqrt(rs - t);
yt = choose(floor(y + d), ceil(y - d), y);
if (yt == MAX) continue;
t += (yt - y) * (yt - y);
if (t - max > eps)
{
max = t;
m_x = i;
m_y = yt;
}
}
cout << m_x << " " << m_y << endl;
}
int main()
{
solve();
return 0;
}