Online Judge | Problem Set | Authors | Online Contests | User | ||||||
---|---|---|---|---|---|---|---|---|---|---|
Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest |
用java的高精度都过不掉,更加强烈建议不做此题数据显然是有争议的,精度问题其实是非常地大。 无论是过掉的程序还是没过掉的的程序,都有精度问题,除法的精度问题不可避免。这关系到浮点在计算机内的表示形式,不管保留几位小数(哪怕只是取整),都有精度问题。 附上JAVA源代码,供指错: import java.text.NumberFormat; import java.util.*; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.PrintStream; import java.math.*; public class Main { static BigDecimal eps=new BigDecimal("0.000000001"); static BigDecimal negeps=new BigDecimal("-0.000000001"); static class point { BigDecimal x,y; void input(Scanner cin) { String s; s=cin.next(); x=new BigDecimal(s); s=cin.next(); y=new BigDecimal(s); } point() { x=new BigDecimal(0); y=new BigDecimal(0); } } static class segment { point p1,p2; void input(Scanner cin) { p1.input(cin); p2.input(cin); } segment() { p1=new point(); p2=new point(); } } static BigDecimal cross(point O,point A,point B) { BigDecimal x1,y1,x2,y2; x1=A.x.subtract(O.x); y1=A.y.subtract(O.y); x2=B.x.subtract(O.x); y2=B.y.subtract(O.y); return x1.multiply(y2).subtract(x2.multiply(y1)); } static int signal(BigDecimal value) { if(value.compareTo(eps)>0) return 1; if(value.compareTo(negeps)<0) return -1; return 0; } static boolean intersection(segment a,segment b) { if(signal(cross(a.p1,b.p1,b.p2))*signal(cross(a.p2,b.p1,b.p2))>0) return false; if(signal(cross(b.p1,a.p1,a.p2))*signal(cross(b.p2,a.p1,a.p2))>0) return false; return true; } static point interpoint(segment a,segment b) { BigDecimal x1,y1,x2,y2,x3,y3,x4,y4,k; x1=a.p1.x; y1=a.p1.y; x2=a.p2.x; y2=a.p2.y; x3=b.p1.x; y3=b.p1.y; x4=b.p2.x; y4=b.p2.y; k=cross(a.p1,b.p1,b.p2).abs().divide(cross(a.p1,b.p1,b.p2).abs().add(cross(a.p2,b.p1,b.p2).abs()),50,BigDecimal.ROUND_HALF_DOWN); point tmp=new point(); tmp.x=x1.subtract(k.multiply(x1.subtract(x2))); tmp.y=y1.subtract(k.multiply(y1.subtract(y2))); return tmp; } /*static point myinterpoint(point a,segment b) { point tmp=new point(); tmp.y=a.y; BigDecimal x1=b.p1.x,y1=b.p1.y,x2=b.p2.x,y2=b.p2.y,x3=a.x,y3=a.y; tmp.x=y2.subtract(y1).divide(x2.subtract(x1)).multiply(y3.subtract(y1)).add(x1); return tmp; }*/ public static void main(String args[]) throws FileNotFoundException { /*BufferedInputStream in = new BufferedInputStream( new FileInputStream( "data.txt")); System.setIn(in);*/ // Produces deprecation message: /*PrintStream out = new PrintStream( new BufferedOutputStream( new FileOutputStream("test.out")));*/ //System.setIn(in); // System.setOut(out); // System.setErr(out); Scanner cin=new Scanner(System.in); while(cin.hasNext()) { int t=cin.nextInt(); for(int i=0;i<t;++i) { segment a=new segment(); segment b=new segment(); a.input(cin); b.input(cin); //System.out.println(a.p1.x+" "+a.p1.y); if((signal(cross(a.p1,b.p1,b.p2))==0) &&(signal(cross(a.p2,b.p1,b.p2))==0)) { //System.out.println("first\n"); System.out.println("0.00"); continue; } if(signal(a.p1.y.subtract(a.p2.y))==0|| signal(b.p1.y.subtract(b.p2.y))==0||(!intersection(a,b))) { //System.out.println("second\n"); //System.out.println(intersection(a,b)); System.out.println("0.00"); continue; } point tmppoint=new point(); if(a.p1.y.compareTo(a.p2.y)<0) { tmppoint=a.p1; a.p1=a.p2; a.p2=tmppoint; } // swap(a.p1,a.p2); if(b.p1.y.compareTo(b.p2.y)<0) { tmppoint=b.p1; b.p1=b.p2; b.p2=tmppoint; } // swap(b.p1,b.p2); point tmp=new point(); tmp.x=a.p1.x; tmp.y=BigDecimal.valueOf(20000.0); segment tmpseg=new segment(); tmpseg.p1=a.p1; tmpseg.p2=tmp; if(intersection(b,tmpseg)) { //System.out.println("third\n"); System.out.println("0.00"); continue; } tmp.x=b.p1.x; tmp.y=BigDecimal.valueOf(20000); tmpseg.p1=b.p1; tmpseg.p2=tmp; if(intersection(a,tmpseg)) { //System.out.println("fourth\n"); System.out.println("0.00"); continue; } point inter=new point(); inter=interpoint(a,b); if(b.p1.y.compareTo(a.p1.y)<0) { segment segtmp=a; a=b; b=segtmp; } tmp.y=a.p1.y;; tmp.x=BigDecimal.valueOf(20000); tmpseg.p1=a.p1; tmpseg.p2=tmp; if(!intersection(tmpseg,b)) { tmp.x=BigDecimal.valueOf(-20000); tmpseg.p2=tmp; } point last=interpoint(tmpseg,b); //point last=interpoint(tmpseg,b); BigDecimal res=last.x.subtract(a.p1.x).multiply(a.p1.y.subtract(inter.y)).divide(BigDecimal.valueOf(2)); res=res.abs(); res=res.setScale(2,BigDecimal.ROUND_HALF_UP); System.out.println(res.toPlainString()); /*double x=res.doubleValue(); NumberFormat ddf1=NumberFormat.getNumberInstance() ; ddf1.setMaximumFractionDigits(2); String s= ddf1.format(res.toPlainString()) ; System.out.print(s);*/ //printf("%.2lf\n",res-eps); } } } } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator