AC-Apple

来源:互联网 发布:浙江大学软件学院就业 编辑:程序博客网 时间:2024/05/16 18:47

Apple

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

Source

输入输出测试

代码块

错误代码

在做这一个题目时,我首先考虑到的是找三角形的外接圆,然后确定外心,确定外接圆的半径。
在确定了外接圆的半径和圆心后,我们就很容易判断第四个点是否在圆内了。
步骤:
1.计算外接圆圆心
2.计算外接圆半径
3.判断点是否在圆内
4.在圆内输出“Rejected”,不在圆内输出“Accepted”
但是最终的结果是错误的!!

//错误代码#include<stdio.h>//runtime error 除零错误   #include<iostream>  #include<math.h>  using namespace std;  int main()  {          int x0,y0,x1,y1,x2,y2,x3,y3;          int x4,y4;          double dis,r;          int n;          cin>>n;          for(int i=0;i<n;i++)          {              cin>>x1>>y1>>x2>>y2>>x3>>y3>>x4>>y4;                          x0 = ((y3-y1)*(y2*y2-y1*y1+x2*x2-x1*x1)+(y2-y1)*(y1*y1-y3*y3+x1*x1-x3*x3))/(2*(x2-x1)*(y3-y1)-2*(x3-x1)*(y2-y1));              y0 = ((x3-x1)*(x2*x2-x1*x1+y2*y2-y1*y1)+(x2-x1)*(x1*x1-x3*x3+y1*y1-y3*y3))/(2*(y2-y1)*(x3-x1)-2*(y3-y1)*(x2-x1));              r  = sqrt((x1-x0)*(x1-x0)+(y1-y0)*(y1-y0));              dis= sqrt((x4-x0)*(x4-x0)+(y4-y0)*(y4-y0));              if(dis <= r)              {               cout<<"Rejected"<<endl;              }              else              {              cout<<"Accepted"<<endl;              }       }      return 0;  } 

正确代码

参考大神代码:http://www.cnblogs.com/chenssy/archive/2012/09/09/2677279.html

下面看这样一个代码

public class ssss {      public static void main(String[] ages){          double d1=2.07;          double d2=1.03;          System.out.println(d1+d2);      }  } 

其输出结果为
3.0999999999999996

虽然计算结果离精确值误差很小,但其不是精确的!这在像如金融计算一样计算精确度要求很高的领域是无法接受的,但这是二进制本身的问题,而计算机普遍采用二进制表示,使用基本数据类型无法解决。

为了解决基本数据类型浮点数不能进行精确计算的问题,Java中专门提供了java.math.BigDecimal类,其提供浮点数的精确计算功能。与BigInteger类相同,其运算操作均使用方法调用完成

以下是java.math.BigDecimal.multiply()方法声明
public BigDecimal multiply(BigDecimal multiplicand)
BigDecimal.valueOf(qty)是把qty这个数转为BigDecimal类型的,BigDecimal是可以处理任意长度的浮点数运算的。

Ⅰ基本函数:
1.valueOf(parament); 将参数转换为制定的类型
String s=”12345”; BigInteger c=BigInteger.valueOf(s); 则c=12345;
BigInteger a=new BigInteger(“23”); BigInteger b=new BigInteger(“34”); a. add(b); 3.subtract(); 相减
5.divide(); 相除取整
6.remainder(); 取余
8.gcd(); 最大公约数
10.negate(); 取反数
11.mod(); a.mod(b)=a%b=a.remainder(b);
13.punlic int comareTo();
14.boolean equals(); 是否相等
15.BigInteger构造函数: 一般用到以下两种: BigInteger(String val); 将指定字符串转换为十进制表示形式; BigInteger(String val,int radix); 将指定基数的 BigInteger 的字符串表示形式转换为 BigInteger
Ⅱ.基本常量: A=BigInteger.ONE 1
C=BigInteger.ZERO 0
Ⅲ.基本操作
1. 读入: 用Scanner类定义对象进行控制台读入,Scanner类在java.util.*包中
Scanner cin=new Scanner(System.in);// 读入 while(cin.hasNext()) //等同于!=EOF { int n; BigInteger m; n=cin.nextInt(); //读入一个int;
m=cin.BigInteger();//读入一个BigInteger; System.out.print(m.toString()); }

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));// 2*(x3-x2)            b = y3.subtract(y2).multiply(BigDecimal.valueOf(2));// 2*(y3-y2)            c = x3.pow(2).subtract(x2.pow(2)).add(y3.pow(2).subtract(y2.pow(2)));              //x3^2-x2^2+(y3^2-y2^2)            e = x2.subtract(x1).multiply(BigDecimal.valueOf(2));//2*(x2-x1)            f = y2.subtract(y1).multiply(BigDecimal.valueOf(2));//2*(y2-y1)            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));//(x1-x0)^2+(y1-y0)^2            dis = (x.subtract(x0)).pow(2).add((y.subtract(y0)).pow(2));            if (dis.compareTo(tt)>0) {//dis.compareTo(tt) 相当于 dis与tt相比较 类比strcmp()                System.out.println("Accepted");            }            else                System.out.println("Rejected");        }    }}
原创粉丝点击