Hallo zusammen,
ich hätte folgendes Programm (siehe Code unten), bei dem kommt der Methodenaufruf return 1 + count1(m) vor. Jetzt bin ich das Ganze mal mit dem Debugger durchgegangen und mir ist aufgefallen, dass dieses "1 +" vor return gar keine Auswirkung hat, oder??
Für was wäre das denn oder wie kann man das verstehen?
Vielen Dank im Voraus.
[CODE lang="java" highlight="9"]public class Binary {
static int count1 (int n) {
if (n == 0) {
return 0;
}
int m = n/2;
if (2*m != n) return 1 + count1(m);
return count1(m);
}
static int count0(int n) {
if (n == 0) return 1;
if (n == 1) return 0;
int m = n%2;
return count0((n-m)/2)+1*m;
}
static boolean equivalent(int n, int m) {
return((count1(n) == count1(m))&&(count0(n) == count0(m)));
}
public static void main(String[] args) {
System.out.println(count1(5));
}
}[/CODE]
ich hätte folgendes Programm (siehe Code unten), bei dem kommt der Methodenaufruf return 1 + count1(m) vor. Jetzt bin ich das Ganze mal mit dem Debugger durchgegangen und mir ist aufgefallen, dass dieses "1 +" vor return gar keine Auswirkung hat, oder??
Für was wäre das denn oder wie kann man das verstehen?
Vielen Dank im Voraus.
[CODE lang="java" highlight="9"]public class Binary {
static int count1 (int n) {
if (n == 0) {
return 0;
}
int m = n/2;
if (2*m != n) return 1 + count1(m);
return count1(m);
}
static int count0(int n) {
if (n == 0) return 1;
if (n == 1) return 0;
int m = n%2;
return count0((n-m)/2)+1*m;
}
static boolean equivalent(int n, int m) {
return((count1(n) == count1(m))&&(count0(n) == count0(m)));
}
public static void main(String[] args) {
System.out.println(count1(5));
}
}[/CODE]