高精度小数第一题
import java.util.*; import java.math.*; public class Main { public static void main(String []args) { Scanner cin=new Scanner(System.in); //求最靠近sqrt(5)的值mid BigDecimal esp=new BigDecimal("0.00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001"); BigDecimal L=new BigDecimal("2"); BigDecimal R=new BigDecimal("3"); BigDecimal mid=L; while(true) { BigDecimal dis=R.subtract(L); if(dis.abs().compareTo(esp)<=0)break; mid=L.add(R).divide(new BigDecimal("2")); if(mid.multiply(mid).compareTo(new BigDecimal("5"))<=0)//mid偏小 L=mid; else R=mid; } //System.out.println(mid); String sa,sb; while(cin.hasNext()) { sa=cin.next(); sb=cin.next(); BigDecimal a=new BigDecimal(sa); BigDecimal b=new BigDecimal(sb); if(a.compareTo(b)>0) { BigDecimal tmp=a; a=b; b=tmp; } BigDecimal k=b.subtract(a); //c=(sqrt(2)+1)/2 BigDecimal c=mid.add(BigDecimal.ONE); c=c.divide(new BigDecimal("2")); BigDecimal res=c.multiply(k); res=res.setScale(0,BigDecimal.ROUND_FLOOR); //System.out.println(res); if(res.equals(a)) System.out.println("0"); else System.out.println("1"); } } }