Algs4-1.1.14实现以2为底的对数

1.1.14编写一个静态方法lg(),接受一个整型参数N,返回不大于log2N的最大整数。不要使用Math库。
答:
public  class Test
{
    public static void main(String[] args)
    {
       int N=Integer.parseInt(args[0]);
       if (N<=0) {StdOut.printf("N must be big than 0."); return;}
       //
       int base=2;
       int power=0;
       int accumulate=1;
    
       while (true)
       {
           if (accumulate==N) break;
           if (accumulate>N) {power--;break;}
           accumulate=accumulate * base;
           power++;
       }
       StdOut.printf("lg"+Integer.toString(N)+"=%d",power);
       }//end main
}//end class
原文地址:https://www.cnblogs.com/longjin2018/p/9848516.html