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
解题思路(错误):
题干保证这四棵树不存在三棵树在一条直线上的情况。
1.求出三棵树所构成外接圆的圆心(根据外心到三点的距离相等);
2.求出外界圆半径r1;
3.求出(x,y)到外接圆圆心的距离r2;
如果r2>r1,“Accepted”;
否则,”Rejected”.
代码(错误):
#include <iostream>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int x1,x2,x3,x,y1,y2,y3,y;
double x0,y0,r1,r2;
cin>>x1>>y1>>x2>>y2>>x3>>y3>>x>>y;
x0=((x1*x1-x2*x2+y1*y1-y2*y2)*(y2-y3)-(x2*x2-x3*x3+y2*y2-y3*y3)*(y1-y2))/(2.0*(x1-x2)*(y2-y3)-2.0*(x2-x3)*(y1-y2));
y0=((x1*x1-x2*x2+y1*y1-y2*y2)*(x2-x3)-(x2*x2-x3*x3+y2*y2-y3*y3)*(x1-x2))/(2.0*(y1-y2)*(x2-x3)-2.0*(y2-y3)*(x1-x2));
r1=(x1-x0)*(x1-x0)+(y1-y0)*(y1-y0);
r2=(x-x0)*(x-x0)+(y-y0)*(y-y0);
if(r2>r1)
cout<<"Accepted"<<endl;
else
cout<<"Rejected"<<endl;
}
return 0;
}
考虑到r1,r2的比较,高精度。
大数问题
大数是指计算的数值非常大或者对运算的精度要求非常高,用已知的数据类型无法精确表示的数值。
例如求Fibonacci数列的第1000个数。
例如计算到小数点后第2000位。
计算大数的一般方法是用数组模拟大数的运算,开一个比较大的整型(或双精度类型)数组,数组的元素代表大数的某一位,通过数组元素的运算模拟大数的运算,最后将代表大数的数组输出。
常用的大数算法有如下几种类型:
大数的加减;
大数的乘;
超大数的乘积;
任意高精度计算。
package 大数问题;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
BigDecimal x1,x2,x3,y1,y2,y3,x,y,x0,y0,r;
BigDecimal a,b,c,d,e,f,g,tt,dis;
Scanner cin = new Scanner(System.in);
int ncase;
ncase = cin.nextInt();
while(ncase-->0)
{
x1 = cin.nextBigDecimal();
y1 = cin.nextBigDecimal();
x2 = cin.nextBigDecimal();
y2 = cin.nextBigDecimal();
x3 = cin.nextBigDecimal();
y3 = cin.nextBigDecimal();
x = cin.nextBigDecimal();
y = cin.nextBigDecimal();
a = x3.subtract(x2).multiply(BigDecimal.valueOf(2));
b = y3.subtract(y2).multiply(BigDecimal.valueOf(2));
c = x3.pow(2).subtract(x2.pow(2)).add(y3.pow(2).subtract(y2.pow(2)));
e = x2.subtract(x1).multiply(BigDecimal.valueOf(2));
f = y2.subtract(y1).multiply(BigDecimal.valueOf(2));
g = x2.pow(2).subtract(x1.pow(2)).add(y2.pow(2).subtract(y1.pow(2)));
x0= g.multiply(b).subtract(c.multiply(f)).divide(e.multiply(b).subtract(a.multiply(f)));
y0 = a.multiply(g).subtract(c.multiply(e)).divide(a.multiply(f).subtract(b.multiply(e)));
tt = (x1.subtract(x0)).pow(2).add((y1.subtract(y0)).pow(2));
dis = (x.subtract(x0)).pow(2).add((y.subtract(y0)).pow(2));
if (dis.compareTo(tt)>0) {
System.out.println("Accepted");
}
else
System.out.println("Rejected");
}
}
}