import java.util.Scanner; import static java.lang.Math.pow; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int e,f; int res; while(sc.hasNext()){ e=sc.nextInt(); f=sc.nextInt(); res=(int)(f/pow(2,e-1)+e-1); //折半查找 System.out.println(res); } } }
第一次修正:考虑蛋比楼多的情况
import java.util.Scanner; import static java.lang.Math.log; import static java.lang.Math.pow; public class Main{ public static void main(String args[]){ Scanner sc=new Scanner(System.in); int e,f; int res; while(sc.hasNext()){ e=sc.nextInt(); f=sc.nextInt(); if (pow(2,e)<=f) res=(int)(f/pow(2,e-1)+e-1); else res=(int)(log(f)/log(2)+1); System.out.println(res); } } }