From 0c8531d9ef119e60a4d6bcc69d61438ec369a818 Mon Sep 17 00:00:00 2001 From: Matti Date: Thu, 11 Apr 2024 10:08:17 +0200 Subject: [PATCH] add 5 --- src/part4/aufg5.java | 55 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/part4/aufg5.java diff --git a/src/part4/aufg5.java b/src/part4/aufg5.java new file mode 100644 index 0000000..915f387 --- /dev/null +++ b/src/part4/aufg5.java @@ -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); + } +} \ No newline at end of file