现在的位置: 首页 > 综合 > 正文

fzu 2036 2036 Log Calculator JAVA 浮点数输出精度控制

2013年03月04日 ⁄ 综合 ⁄ 共 1197字 ⁄ 字号 评论关闭

http://acm.fzu.edu.cn/problem.php?pid=2036

a>b
a=log2(x)   x=2^a
b=log2(y)   y=2^b
log2(x+y)=b+log2(1+2^(a-b));

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 *
 * @author acm
 */
import java.util.*;
import java.math.*;
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        Scanner cin=new Scanner(System.in);
        int ci=cin.nextInt();
        int pl=1;
        while((ci--)!=0){
            BigDecimal a,b;
            a=cin.nextBigDecimal();
            b=cin.nextBigDecimal();
            if(a.compareTo(b)<0){
                BigDecimal t=a;
                a=b;
                b=t;
            }
            BigDecimal ans=b;
            BigDecimal deta=a.subtract(b);
            if(deta.compareTo(BigDecimal.valueOf(32))>0){
                ans=ans.add(deta);
            }else{
                double p=deta.doubleValue();
                double cnt=Math.pow(2.0, p);
                double tmp=Math.log(1+cnt)/Math.log(2.0);
                ans=ans.add(BigDecimal.valueOf(tmp));
            }
            System.out.print("Case "+(pl++)+": ");
            System.out.println(ans.setScale(9, BigDecimal.ROUND_HALF_UP));//保留小数点后9位
        }
    }
}
/*
 *   double a=0.036*100;
                DecimalFormat df=new DecimalFormat("0.000");
                System.out.println(df.format(a));//保留小数点后3位 
  */

抱歉!评论已关闭.