This commit is contained in:
Matti 2024-04-11 10:08:17 +02:00
parent 1b367eebc6
commit 0c8531d9ef

55
src/part4/aufg5.java Normal file
View File

@ -0,0 +1,55 @@
package part4;
import java.util.Arrays;
public class aufg5 {
// =====================
// Der Goldfrosch
// =====================
public static void main(String[] args){
int no_frogs = Integer.MAX_VALUE;
int ARRAY_LENGTH = 46; // 46 is sufficient for Int.MaxValue
int current_pos;
int max_pos = 1;
int bucket0 = 0;
int bucket_Neg_1 = 0;
int[] field = new int[ARRAY_LENGTH];
Arrays.fill(field,1);
for(int i = 0; i < no_frogs; i++){
current_pos = 1;
while(current_pos > 0) {
// flip arrow
if (field[current_pos + 1] == 1) {
field[current_pos + 1] = -2;
} else if (field[current_pos + 1] == -2) {
field[current_pos + 1] = 1;
}
// jump
current_pos += field[current_pos + 1];
if (current_pos > max_pos) {
max_pos = current_pos;
}
}
// end
if(current_pos == 0){
bucket0 += 1;
} else if (current_pos == -1){
bucket_Neg_1 += 1;
}
}
System.out.println("Anzahl Frösche: " + no_frogs);
System.out.println("Der Froschrekord liegt bei Feld: " + max_pos); //might be 1 less than actual record
System.out.println("Eimer auf Feld -1: " + bucket_Neg_1);
System.out.println("Eimer auf Feld 0: " + bucket0);
}
}