HDU 6206 Apple (计算几何_三点求圆 & 高精度浮点数 & Java大数_BigDecimal)

11 篇文章 0 订阅
4 篇文章 0 订阅

传送门

题意:确定第四个点是否在前三个点形成的圆以外,若是输出"Accepted",否则输出"Rejected"。

思路:这里应用其他大佬的证明过程(截图偏于保存,细则可移步查看)。

有了圆心 (x_{0}y_{0}),再带入任意一点便能求得半径值了。

但是由于这个题的精度爆double,于是采取Java的大数BigDecimal来求解,这里先展示C++的代码思路结构。

C++代码实现:

#include<bits/stdc++.h>
#define endl '\n'
#define null NULL
#define ll long long
#define int long long
#define pii pair<int, int>
#define lowbit(x) (x &(-x))
#define ls(x) x<<1
#define rs(x) (x<<1+1)
#define me(ar) memset(ar, 0, sizeof ar)
#define mem(ar,num) memset(ar, num, sizeof ar)
#define rp(i, n) for(int i = 0, i < n; i ++)
#define rep(i, a, n) for(int i = a; i <= n; i ++)
#define pre(i, n, a) for(int i = n; i >= a; i --)
#define IOS ios::sync_with_stdio(0); cin.tie(0);cout.tie(0);
const int way[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
using namespace std;
const int  inf = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-12;
const ll   mod = 1e9+7;
const int  N = 2e5 + 5;

inline void read(int &x){
    char t=getchar();
    while(!isdigit(t)) t=getchar();
    for(x=t^48,t=getchar();isdigit(t);t=getchar()) x=x*10+(t^48);
}

int t;
double p[5][5], ox, oy, r;

double dis(double ax, double ay, double bx, double by){
    return sqrt((ax-bx)*(ax-bx)+(ay-by)*(ay-by));
}

void get(double ax, double ay, double bx, double by, double cx, double cy){
    double a = ax-bx, b = ay-by;
    double c = ax-cx, d = ay-cy;
    double e = (ax*ax-bx*bx+ay*ay-by*by)/2;
    double f = (ax*ax-cx*cx+ay*ay-cy*cy)/2;
    ox = (e*d-b*f)/(a*d-a*c);
    oy = (f*a-c*e)/(a*d-b*c);
    r = dis(ox, oy, ax, ay);
}

signed main()
{
    IOS;

    cin >> t;
    while(t --){
        for(int i = 1; i <= 4; i ++){
            cin >> p[i][1] >> p[i][2];
        }
        get(p[1][1], p[1][2], p[2][1], p[2][2], p[3][1], p[3][2]);
        double d = dis(ox, oy, p[4][1], p[4][2]);
        cout << (d>r? "Accepted":"Rejected") << endl;
    }

    return 0;
}

 

JAVA代码实现:

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

public class Main{

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        BigDecimal [][] p = new BigDecimal[5][5];
        BigDecimal two=new BigDecimal("2.0");
        BigDecimal ox, oy;
        int t = in.nextInt();
        while(t-->0){
            for(int i = 1; i <= 4; i ++){
                p[i][1] = in.nextBigDecimal();
                p[i][2] = in.nextBigDecimal();
            }
            BigDecimal ax = p[1][1], ay = p[1][2];
            BigDecimal bx = p[2][1], by = p[2][2];
            BigDecimal cx = p[3][1], cy = p[3][2];
            BigDecimal x1 = two.multiply(ax.subtract(bx)), y1 = two.multiply(ay.subtract(by));
            BigDecimal d1 = ax.multiply(ax).subtract(bx.multiply(bx)).add(ay.multiply(ay)).subtract(by.multiply(by));
            BigDecimal x2 = two.multiply(ax.subtract(cx)), y2 = two.multiply(ay.subtract(cy));
            BigDecimal d2 = ax.multiply(ax).subtract(cx.multiply(cx)).add(ay.multiply(ay)).subtract(cy.multiply(cy));
            BigDecimal tmp = x1.multiply(y2).subtract(y1.multiply(x2));
            ox = d1.multiply(y2).subtract(y1.multiply(d2)).divide(tmp);
            oy = d2.multiply(x1).subtract(x2.multiply(d1)).divide(tmp);

            BigDecimal a = ox.subtract(ax), b = oy.subtract(ay);
            BigDecimal r = a.multiply(a).add(b.multiply(b));

            BigDecimal c = ox.subtract(p[4][1]), d = oy.subtract(p[4][2]);
            BigDecimal  dis = c.multiply(c).add(d.multiply(d));

            if(dis.compareTo(r)==1) System.out.println("Accepted");
            else System.out.println("Rejected");
        }
        in.close();
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值