0
我不明白爲什麼它的工作原理是這樣,希望有人能幫助我做一些詳細的解釋,太感謝你了關於返回不大於N的基數2對數的最大整數。有人可以解釋爲什麼下面的代碼工作?
public class Ex14 {
public static int lg(int N) {
int i, n;
for (i = 0, n = 1; n <= N; i++) {
n *= 2;
}
return i - 1;
}
public static void main(String[] args) {
System.out.println(lg(1025));
}
}
你有什麼不明白的? – user2023608
@ user2023608 x = log2N,其中x是我們需要得到的答案,並且其意味着N = 2 * 2 * 2 ......(x次),但是爲什麼在這裏使用n = n * 2 –