题意:给定和求
思路:假设
则
如果a-b很大的话(我们是假设a-b>100),2的a-b次方可以看作和2的a-b次方加1相等。所以直接输出a就行了,否则暴力算出x和y再求出答案。
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
public class Main{
public static void main(String args[]){
BigDecimal a, b;
Scanner cin = new Scanner(System.in);
int cas = cin.nextInt();
int kk = 0;
while(true) {
if(cas == 0) break;
--cas;
a = cin.nextBigDecimal();
b = cin.nextBigDecimal();
if(a.compareTo(b) < 0) {
BigDecimal t = a;
a = b;
b = t;
}
System.out.print("Case " + (++kk) + ": ");
if(a.subtract(b).compareTo(BigDecimal.valueOf(100.0)) > 0) {
System.out.println(a.setScale(9, BigDecimal.ROUND_HALF_UP));
continue;
}
BigDecimal TWO = BigDecimal.valueOf(2.0);
double two = 2.0;
double x = Math.pow(two, a.subtract(b).doubleValue());
x = x + 1;
double y = Math.log(x) / Math.log(2);
BigDecimal ans = b.add(BigDecimal.valueOf(y));
System.out.println(ans.setScale(9, BigDecimal.ROUND_HALF_UP));
}
}
}
搜索
复制