Apple HDU - 6206(JAVA高精度求三角形外心)

本文介绍了一种算法,用于判断给定点是否位于由三个已知点形成的圆的外部。通过解析输入数据,利用高精度计算避免数值溢出,最终确定点的位置关系。

Apple HDU - 6206

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
题意:

给定四个点坐标,问最后一个点在前三个点构成的三角形的外接圆的园内还是圆外

分析:

这里写图片描述

因为每个数的绝对值是1e12所以再求距离的时候一定会有平方变成1e24,所以要用java高精度

code:

import java.util.*;
import java.math.*;

public class Main
{
    public static BigDecimal fun(BigDecimal x)
    {
        return x.multiply(x);
    }
    public static void main(String []args)
    {
        Scanner in = new Scanner(System.in);
        int Tcase = in.nextInt();
        // System.out.println(Tcase);
        while ( (Tcase --) > 0 )
        {

            BigDecimal 
            x1 = in.nextBigDecimal() ,y1 = in.nextBigDecimal(),
            x2 = in.nextBigDecimal() ,y2 = in.nextBigDecimal(),
            x3 = in.nextBigDecimal() ,y3 = in.nextBigDecimal(),
            x4 = in.nextBigDecimal() ,y4 = in.nextBigDecimal();


            BigDecimal a = y3.subtract(y2).multiply(new BigDecimal(2) ).multiply( x2.multiply(x2).add(y2.multiply(y2)) .subtract( x1.multiply(x1)).subtract( y1.multiply(y1)));
            BigDecimal b = y2.subtract(y1).multiply(new BigDecimal(2)).multiply( x3.multiply(x3).add( y3.multiply(y3)).subtract( x2.multiply(x2)).subtract(y2.multiply(y2)));
            BigDecimal c = y3.subtract(y2).multiply(new BigDecimal(4)).multiply(x2.subtract(x1));
            BigDecimal d = y2.subtract(y1).multiply(new BigDecimal(4)).multiply(x3.subtract(x2));

            BigDecimal x = a.subtract(b).divide(c.subtract(d));
            // System.out.println(a + " " + b + " " + c + " " + d );

            a = x3.subtract(x2).multiply(new BigDecimal(-2)).multiply( x2.multiply(x2).add( y2.multiply(y2)).subtract(x1.multiply(x1)).subtract( y1.multiply(y1)));
            b = x2.subtract(x1).multiply(new BigDecimal(2)).multiply( x3.multiply(x3).add(y3.multiply(y3)).subtract(x2.multiply(x2)).subtract( y2.multiply(y2)));
            c = y3.subtract(y2).multiply(new BigDecimal(4)).multiply(x2.subtract(x1));
            d = y2.subtract(y1).multiply(new BigDecimal(4)).multiply(x3.subtract(x2));

            BigDecimal y = a.add(b).divide(c.subtract(d));

            BigDecimal r = fun(x.subtract(x1)).add( fun(y.subtract(y1)) );
            BigDecimal temp = fun(x.subtract(x4)).add(fun(y.subtract(y4)) );

            // System.out.println(x + " " + y);
            if(temp.compareTo(r) == 1)System.out.println("Accepted");
            else System.out.println("Rejected");
        }
    }
}

### 关于HDU-1443问题的Java实现 针对HDU平台上的题目ID为1054的问题,在64位整数输入输出格式方面应采用`%lld`,而Java类名需指定为主类名为`Main`[^1]。 对于解决该问题的一个实例是在处理两个数组中寻找重复数字的任务时。在Java方法内部可以利用`duplication[0]`来替代C/C++里的指针形式`*duplication`,这使得可以在返回布尔值的同时获取所需的数值[^2]。 下面给出一段适用于此场景下的Java代码片段: ```java import java.util.HashSet; import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); HashSet<Integer> set = new HashSet<>(); while (scanner.hasNextInt()) { int num = scanner.nextInt(); if (!set.add(num)) { // 如果add失败,则说明已经存在这个元素 System.out.println("Duplicate number found: " + num); break; // 或者继续收集所有的重复项取决于具体需 } } scanner.close(); } private static boolean duplicate(int numbers[], int length, int duplication[]) { if (numbers == null || length <= 0) return false; for (int i = 0; i < length; ++i) { while (numbers[i] != i) { if (numbers[i] == numbers[numbers[i]]) { duplication[0] = numbers[i]; return true; } else { // Swap elements to their correct positions. int temp = numbers[numbers[i]]; numbers[numbers[i]] = numbers[i]; numbers[i] = temp; } } } return false; } } ``` 这段程序首先定义了一个`HashSet`用于存储读取到的数据,并通过尝试向集合中添加新元素的方式来检测是否存在重复数据;一旦发现有重复即刻打印并停止进一步操作。另外还提供了一个辅助函数`duplicate()`用来判断给定数组内是否有任何重复元素,并将找到的第一个重复值存入传入参数`duplication[]`的第一位置上以便调用方访问。 需要注意的是上述代码仅作为示例展示如何运用特定技巧解决问题,并不一定完全符合原题目的所有约束条件以及边界情况处理,请根据实际题目描述调整逻辑细节以满足要
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值