Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Aufgabe 3) Rekursion mit eindimensionalen Arrays
- */
- import java.util.Arrays;
- public class Aufgabe3 {
- private static int getMaxPairSum(int[] workArray, int start, int end) {
- // TODO: Implementieren Sie hier Ihre Lösung für die Methode
- if (start<end){
- return Math.max((workArray[start]+workArray[start+1]),(getMaxPairSum(workArray,start+1,end)));
- }
- return 0; //Zeile kann geändert oder entfernt werden.
- }
- private static int countValuesWithSmallerNeighbors(int[] workArray, int index) {
- // TODO: Implementieren Sie hier Ihre Lösung für die Methode
- if (index<workArray.length-1){
- if (workArray[index]>workArray[index-1]) return 1+countValuesWithSmallerNeighbors(workArray,index+1);
- else return 0+countValuesWithSmallerNeighbors(workArray,index+1);
- }
- return 0; //Zeile kann geändert oder entfernt werden.
- }
- private static int[] getArrayWithNegativeValues(int[] workArray, int index) {
- // TODO: Implementieren Sie hier Ihre Lösung für die Methode
- int[] copy=workArray.clone();
- if (index<=workArray.length-1){
- if (copy[index]>0) copy[index]=0;
- return getArrayWithNegativeValues(copy,index+1);
- }
- return copy;
- }
- private static boolean containsValue(int[] workArray, int value) {
- // TODO: Implementieren Sie hier Ihre Lösung für die Methode
- if (workArray.length>0){
- if (workArray[0]==value) return true;
- else if(workArray.length!=1){
- return containsValue(Arrays.copyOfRange(workArray,0,workArray.length/2),value)||containsValue(Arrays.copyOfRange(workArray,workArray.length/2,workArray.length),value);
- }
- }
- return false;
- }
- public static void main(String[] args) {
- int[] array1 = {3, 1, 7, 9, 4, 10, 5, 3};
- System.out.println(getMaxPairSum(array1, 0, array1.length - 1));
- System.out.println(getMaxPairSum(array1, 4, array1.length - 1));
- System.out.println(getMaxPairSum(array1, 1, 3));
- System.out.println(getMaxPairSum(array1, 0, 2));
- System.out.println(getMaxPairSum(array1, 0, 1));
- System.out.println();
- int[] array2 = {3, 9, 7, 19, 8, 10, 6, 3, 11, 5};
- System.out.println(countValuesWithSmallerNeighbors(array2, 1));
- System.out.println(countValuesWithSmallerNeighbors(array2, 4));
- System.out.println(countValuesWithSmallerNeighbors(array2, 6));
- System.out.println(countValuesWithSmallerNeighbors(array2, 8));
- System.out.println();
- int[] array3 = {5, -3, 7, -15, 20, -5, 0, 14, 3, -2, -8};
- System.out.println(Arrays.toString(array3));
- System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 0)));
- System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 10)));
- System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 8)));
- System.out.println(Arrays.toString(getArrayWithNegativeValues(array3, 4)));
- System.out.println();
- int[] array4 = {2, 4, 7, 10, -10, 4, 0, 0, 27, 11, 4, 6};
- System.out.println(containsValue(array4, 11));
- System.out.println(containsValue(array4, 2));
- System.out.println(containsValue(array4, 25));
- System.out.println(containsValue(array4, 0));
- System.out.println(containsValue(array4, 14));
- System.out.println(containsValue(array4, 6));
- assert(getMaxPairSum(array1, 0, array1.length - 1) == 16);
- assert(getMaxPairSum(array1, 4, array1.length - 1) == 15);
- assert(getMaxPairSum(array1, 1, 3) == 16);
- assert(getMaxPairSum(array1, 0, 2) == 8);
- assert(getMaxPairSum(array1, 0, 1) == 4);
- assert(countValuesWithSmallerNeighbors(array2, 1) == 4);
- assert(countValuesWithSmallerNeighbors(array2, 4) == 2);
- assert(countValuesWithSmallerNeighbors(array2, 6) == 1);
- assert(countValuesWithSmallerNeighbors(array2, 8) == 1);
- assert (Arrays.equals(getArrayWithNegativeValues(array3, 0), new int[]{0, -3, 0, -15, 0, -5, 0, 0, 0, -2, -8}) == true);
- assert (Arrays.equals(getArrayWithNegativeValues(array3, 10), new int[]{5, -3, 7, -15, 20, -5, 0, 14, 3, -2, -8}) == true);
- assert (Arrays.equals(getArrayWithNegativeValues(array3, 8), new int[]{5, -3, 7, -15, 20, -5, 0, 14, 0, -2, -8}) == true);
- assert (Arrays.equals(getArrayWithNegativeValues(array3, 4), new int[]{5, -3, 7, -15, 0, -5, 0, 0, 0, -2, -8}) == true);
- assert (containsValue(array4, 11) == true);
- assert (containsValue(array4, 2) == true);
- assert (containsValue(array4, 25) == false);
- assert (containsValue(array4, 0) == true);
- assert (containsValue(array4, 14) == false);
- assert (containsValue(array4, 6) == true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement