就是卡精度, 我把sqrt() 都去了, 换成平方与平方之间的比较, AC
思路:
1. 题目可以转换成判断线段与圆的关系。
那么你只要找到线段上离圆心最近的点,然后计算它与圆心的距离是否小于半径。
2. 要特判,线段是否包含于圆,是则线段依然不与圆交。
代码:
#include <iostream> #include <cstdio> #include <cmath> #include <algorithm> using namespace std; const double EP = 1e-8; struct Point{ double x,y; }p1 , p2 , p3, p4 ; struct Cirlce{ double x,y,r; }c; double cal( double x, double y ){//点到圆心的距离的平方 return (x-c.x)*(x-c.x) + (y-c.y)*(y-c.y); } double sol_3x(double p , double Left, double Right ){ while( Left + EP < Right ){ double m1 = ( Left*2.0 + Right ) / 3.0; double m2 = ( Left + Right*2.0 ) / 3.0; if( cal( p ,m1 ) > cal( p ,m2 )) Left = m1; else Right = m2; } return Left; } double sol_3y(double p , double Left, double Right ){ while( Left + EP < Right ){ double m1 = ( Left*2.0 + Right ) / 3.0; double m2 = ( Left + Right*2.0 ) / 3.0; if( cal( m1 , p ) > cal( m2, p )) Left = m1; else Right = m2; } return Left; } bool isIntersect( Point p1, Point p2 ){ double most; if( cal(p1.x,p1.y) < c.r*c.r && cal(p2.x,p2.y) < c.r*c.r ) return false; if( p1.x == p2.x ) { if( p1.y > p2.y ) swap( p1, p2 ); most = sol_3x( p1.x , p1.y , p2.y ); if( cal(p1.x,most ) <= c.r*c.r ) return true; } else { if( p1.x > p2.x ) swap(p1,p2); most = sol_3y( p1.y, p1.x, p2.x); if( cal(most,p1.y) <= c.r*c.r ) return true; } return false; } void solve(){ p2.x = p3.x; p2.y = p1.y; p4.x = p1.x; p4.y = p3.y; if( ( isIntersect( p1, p2) || isIntersect( p2,p3) || isIntersect(p3,p4) || isIntersect(p4,p1) )) {puts("YES");return;} puts("NO"); return ; } int main(){ int n; scanf("%d",&n); while(n--){ scanf("%lf%lf%lf%lf%lf%lf%lf",&c.x,&c.y,&c.r,&p1.x,&p1.y,&p3.x,&p3.y ); solve(); } return 0; }