Witam
Mam problem ze zrozumieniem poniższego kodu, a szczególnie momentu, w którym dochodzą dwie zmienne wejściowe jak "max" i "current". Przykładowo dla liczby 12 z Array (test), pierwszy if = false, drugi true, bo reszta z dzielenia 12/2 równa się 0, natomiast o co chodzi z max i current?
public class Main {
public static void main(String[] args) {
int[] tests = { 9, 37, 0b1000001010001, 12 };
for (int i : tests)
System.out.printf("input = %d, Binary form = %s, Answer = %d%n",
i , Integer.toBinaryString(i), solution(i));
}
static int solution(int n){
return solution(n, 0, 0);
}
static int solution(int n, int max, int current) {
if (n == 0)
return max;
else if (n % 2 == 0)
return solution(n / 2, max, current + 1);
else
return solution(n / 2, Math.max(max, current), 0);
}
}
Po kompilacji dostajemy taki wynik:
input = 9, Binary form = 1001, Answer = 2
input = 37, Binary form = 100101, Answer = 2
input = 4177, Binary form = 1000001010001, Answer = 5
input = 12, Binary form = 1100, Answer = 2