fzu 2036 2036 Log Calculator JAVA 浮点数输出精度控制
2011-08-17 19:19
375 查看
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位
*/
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位
*/
相关文章推荐
- Fzu 2036 Log Calculator(JAVA 浮点数输出精度控制)
- java输出精度控制
- C和Java中如何控制浮点数的精度输出
- C++中有关浮点数输出精度的控制
- HDU 5112 A Curious Matt (Java comparator + 快速IO + 控制浮点精度)
- Java的数值精度的输出控制(包含BigDecimal)
- Java控制输出精度
- java浮点运算精度问题的解决
- java float double精度为什么会丢失?浅谈java的浮点数精度问题 【转】
- Java基础应用之循环控制(使用嵌套循环在控制台上输出九九乘法表)
- Java中使用System.out.printf().如何控制输出格式及类型不一致的解决方法
- C++输出精度控制
- 方法级别的java日志输出控制(一)
- Java 浮点数 float和double类型的表示范围和精度
- 面试题:java中的访问控制符优先级和输出6+6+"java"+6+6的结果
- JSTL、JAVA将浮点数字输出格式化
- java控制float和double的精度
- 在Java中,控制一个double数以两位小数形式输出!
- c++输出精度控制
- Java中sleep的控制精度问题以及深入理解