HDU 6206 Apple (高精确度+JAVA BigDecimal)

时间:2024-09-15 15:06:20
Problem Description
Apple is Taotao's favourite fruit. In his backyard, there are three apple trees with coordinates (x1,y1), (x2,y2), and (x3,y3). Now Taotao is planning to plant a new one, but he is not willing to take these trees too close. He believes that the new apple tree should be outside the circle which the three apple trees that already exist is on. Taotao picked a potential position (x,y) of the new tree. Could you tell him if it is outside the circle or not?
Input
The first line contains an integer T, indicating that there are T(T≤30) cases.
In the first line of each case, there are eight integers x1,y1,x2,y2,x3,y3,x,y, as described above.
The absolute values of integers in input are less than or equal to 1,000,000,000,000.
It is guaranteed that, any three of the four positions do not lie on a straight line.
Output
For each case, output "Accepted" if the position is outside the circle, or "Rejected" if the position is on or inside the circle.
Sample Input
3
-2 0 0 -2 2 0 2 -2
-2 0 0 -2 2 0 0 2
-2 0 0 -2 2 0 1 1
Sample Output
Accepted
Rejected
Rejected
题目大意:
  给三个不在同一条直线上的点的坐标,判断第四个点在不在这三个点确定的圆的圆内,如果在,就输出Rejected,不然输出Accepted。
思路:
  一开始是用C++,疯狂调ESP的参数,但是wa了10多法发现是在是不好做,又看到大家都在用JAVA,所以就想到了JAVA里的BigDecimal类可以进行大数据高精确度处理。因为是第一次在OJ上提交JAVA,开始虽然写对了,但是也老是wa。后来发现在HDU中,主类名的Main要求M必须大写,开始叫了好几发main于是老是错而不是报CE。。。所以就记录下来,以备以后使用
AC代码:
import  java.math.*;
import java.util.*; public class Main {
public static void main(String[] args) { Scanner cin=new Scanner(System.in);// 读入
int T;
T=cin.nextInt();
for(int z=0;z<T;z++){
Point a=new Point();
Point b=new Point();
Point c=new Point();
a.x=cin.nextBigDecimal();
a.y=cin.nextBigDecimal();
b.x=cin.nextBigDecimal();
b.y=cin.nextBigDecimal();
c.x=cin.nextBigDecimal();
c.y=cin.nextBigDecimal();
Point o=waixin(a,b,c);
// BigDecimal r1=dis(o,a); Point d=new Point();
d.x=cin.nextBigDecimal();
d.y=cin.nextBigDecimal(); // if(dis(o, d).compareTo(dis(o,a)) == -1) System.out.println("Rejected");
// else if(dis(o, d).compareTo(dis(o,a)) == -1) System.out.println("Rejected");
// else System.out.println("Accepted");
if(dis(o, d).compareTo(dis(o,a)) == 1) System.out.println("Accepted");
else System.out.println("Rejected"); }
} private static class Point{
public BigDecimal x, y;
public Point(){}
public Point(BigDecimal _x, BigDecimal _y)
{
x = _x; y = _y;
}
} static BigDecimal dis(Point a, Point b)
{
// printf("a.x:%llf b.x%llf a.y:%lf b.y:%lf\n", a.x, b.x, a.y, b.y);
// return (a.x.subtract(b.x))
return (a.x.subtract(b.x)).pow(2).add((a.y.subtract(b.y)).pow(2));
// return (a.x-b.x)*(a.x-b.x);
} static Point waixin(Point a, Point b, Point c)
{
BigDecimal temp =BigDecimal.valueOf(2);
BigDecimal a1 = b.x.subtract(a.x), b1 = b.y.subtract(a.y), c1 = (a1.pow(2).add(b1.pow(2))).divide(temp);
BigDecimal a2 = c.x.subtract(a.x), b2 = c.y.subtract(a.y), c2 = (a2.pow(2).add(b2.pow(2))).divide(temp);
BigDecimal d = (a1.multiply(b2).subtract(a2.multiply(b1)));
Point ret=new Point(a.x.add(c1.multiply(b2).subtract(c2.multiply(b1)).divide(d)),a.y.add(a1.multiply(c2).subtract(a2.multiply(c1)).divide(d)));
// return Point(a.x.add(c1.multiply(b2).subtract(c2.multiply(b1)).divide(d)),a.y.add(a1.multiply(c2).subtract(a2).multiply(c1).divide(d)));
// return Point(a.x + (c1*b2 - c2*b1)/d, a.y + (a1*c2 - a2*c1)/d);
return ret;
} }