I can't find mistakes. Help me, please!
Здравейте,
опитах се да реша задача №11 / Technology Fundamentals - септември 2018 / JAVA
Нулевите тестове за ОК. Пробвах и различни други входнни данни и кода работи ОК. Резултатът обаче е 30/100;
Прегледах видеото от упражненията. Там решават задачата с резултат 10/100; т.е. не ми върши работа защото аз съм малко по-добре.
Къде бъркам? Нещо голямо ще е да е защото ми се чупят 70% от тестовете но не мога да го видя.
Ето кода. Малко е дълъг за което съжалявам. От два месеца програмирам и не мога да пиша все още оптимизирани кодове.
МОЛЯ ПОМОГНЕТЕ МИ!!! УМИРАМ ОТ ЯД И МИ ТРЯБВА РЕШЕНИЕ ЗАЩОТО ЩЕ ГРЪМНА!
Мерси предварително.
Сашко!
import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class ArrayManipulator { public static void main(String[] args) throws IOException { BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); int[] arrayOfNumbers = Arrays.stream(reader.readLine().split(" ")) .mapToInt(Integer::parseInt).toArray(); List<Integer> listOfInt = new ArrayList<>(); String line = reader.readLine(); while (!line.equalsIgnoreCase("End")) { String[] lineArray = line.split(" "); String command = lineArray[0]; switch (command) { case "exchange": int index = Integer.parseInt(lineArray[1]); if (index < 0 || index > arrayOfNumbers.length - 1) { System.out.println("Invalid index"); } else { exchange(arrayOfNumbers, index); } break; case "max": switch (lineArray[1]) { case "even": int num = findMaxEvenIndex(arrayOfNumbers); if (num < 0) { System.out.println("No matches"); } else { System.out.println(num); } break; case "odd": num = findMaxOddIndex(arrayOfNumbers); if (num < 0) { System.out.println("No matches"); } else { System.out.println(num); } break; } break; case "min": switch (lineArray[1]) { case "even": int num = findMinEvenIndex(arrayOfNumbers); if (num < 0) { System.out.println("No matches"); } else { System.out.println(num); } break; case "odd": num = findMinOddIndex(arrayOfNumbers); if (num < 0) { System.out.println("No matches"); } else { System.out.println(num); } break; } break; case "first": switch (lineArray[2]) { case "even": int countIndex = Integer.parseInt(lineArray[1]); if(countIndex > arrayOfNumbers.length){ System.out.println("Invalid count"); }else{ countFirstEven(arrayOfNumbers,countIndex); } break; case "odd": countIndex = Integer.parseInt(lineArray[1]); if(countIndex > arrayOfNumbers.length){ System.out.println("Invalid count"); }else{ countFirstOdd(arrayOfNumbers,countIndex); } break; } break; case "last": switch (lineArray[2]) { case "even": int countIndex = Integer.parseInt(lineArray[1]); if(countIndex > arrayOfNumbers.length){ System.out.println("Invalid count"); }else{ countLastEven(arrayOfNumbers,countIndex); } break; case "odd": countIndex = Integer.parseInt(lineArray[1]); if(countIndex > arrayOfNumbers.length){ System.out.println("Invalid count"); }else{ countLastOdd(arrayOfNumbers,countIndex); } break; } break; } line = reader.readLine(); } printArray(arrayOfNumbers); } private static void countLastOdd(int[] arrayOfNumbers, int countIndex) { int countLastOddNum = 0; List<Integer> listOfInt = new ArrayList<>(); for (int i = 0; i <arrayOfNumbers.length ; i++) { if(arrayOfNumbers[arrayOfNumbers.length - 1 - i] % 2 == 1){ countLastOddNum++; listOfInt.add(arrayOfNumbers[arrayOfNumbers.length - 1 - i]); if(countLastOddNum == countIndex){ break; } } } arrayOfNumbers = new int[listOfInt.size()]; for (int i = 0; i <arrayOfNumbers.length ; i++) { arrayOfNumbers[i] = listOfInt.get(i); } printArray(arrayOfNumbers); } private static void countLastEven(int[] arrayOfNumbers, int countIndex) { int countLastEvenNum = 0; List<Integer> listOfInt = new ArrayList<>(); for (int i = 0; i <arrayOfNumbers.length ; i++) { if(arrayOfNumbers[arrayOfNumbers.length - 1 - i] % 2 == 0){ countLastEvenNum++; listOfInt.add(arrayOfNumbers[arrayOfNumbers.length - 1 - i]); if(countLastEvenNum == countIndex){ break; } } } arrayOfNumbers = new int[listOfInt.size()]; for (int i = 0; i <arrayOfNumbers.length ; i++) { arrayOfNumbers[i] = listOfInt.get(i); } printArray(arrayOfNumbers); } private static void countFirstOdd(int[] arrayOfNumbers, int countIndex) { int countFirstOddNum = 0; List<Integer> listOfInt = new ArrayList<>(); for (int i = 0; i <arrayOfNumbers.length ; i++) { if(arrayOfNumbers[i] % 2 == 1){ countFirstOddNum++; listOfInt.add(arrayOfNumbers[i]); if(countFirstOddNum == countIndex){ break; } } } arrayOfNumbers = new int[listOfInt.size()]; for (int i = 0; i <arrayOfNumbers.length ; i++) { arrayOfNumbers[i] = listOfInt.get(i); } printArray(arrayOfNumbers); } private static void countFirstEven(int[] arrayOfNumbers, int countIndex) { int countFirstEvenNum = 0; List<Integer> listOfInt = new ArrayList<>(); for (int i = 0; i <arrayOfNumbers.length ; i++) { if(arrayOfNumbers[i] % 2 == 0){ countFirstEvenNum++; listOfInt.add(arrayOfNumbers[i]); if(countFirstEvenNum == countIndex){ break; } } } arrayOfNumbers = new int[listOfInt.size()]; for (int i = 0; i <arrayOfNumbers.length ; i++) { arrayOfNumbers[i] = listOfInt.get(i); } printArray(arrayOfNumbers); } private static int findMinOddIndex(int[] arrayOfNumbers) { int index = -1; int smallestNum = Integer.MAX_VALUE; for (int i = 0; i < arrayOfNumbers.length; i++) { if (arrayOfNumbers[i] % 2 == 1) { if (arrayOfNumbers[i] < smallestNum) { smallestNum = arrayOfNumbers[i]; index = i; } } } return index; } private static int findMinEvenIndex(int[] arrayOfNumbers) { int index = -1; int smallestNum = Integer.MAX_VALUE; for (int i = 0; i < arrayOfNumbers.length; i++) { if (arrayOfNumbers[i] % 2 == 0) { if (arrayOfNumbers[i] < smallestNum) { smallestNum = arrayOfNumbers[i]; index = i; } } } return index; } private static int findMaxOddIndex(int[] arrayOfNumbers) { int index = -1; int biggestNum = Integer.MIN_VALUE; for (int i = 0; i < arrayOfNumbers.length; i++) { if (arrayOfNumbers[i] % 2 == 1) { if (arrayOfNumbers[i] > biggestNum) { biggestNum = arrayOfNumbers[i]; index = i; } } } return index; } private static int findMaxEvenIndex(int[] arrayOfNumbers) { int index = -1; int biggestNum = Integer.MIN_VALUE; for (int i = 0; i < arrayOfNumbers.length; i++) { if (arrayOfNumbers[i] % 2 == 0) { if (arrayOfNumbers[i] > biggestNum) { biggestNum = arrayOfNumbers[i]; index = i; } } } return index; } private static void printArray(int[] arrayOfNumbers) { System.out.print("["); for (int i = 0; i < arrayOfNumbers.length; i++) { if (i != arrayOfNumbers.length - 1) { System.out.print(arrayOfNumbers[i] + ", "); } else { System.out.print(arrayOfNumbers[i]); } } System.out.println("]"); } private static void exchange(int[] arrayOfNumbers, int index) { int[] newArray = new int[arrayOfNumbers.length]; for (int i = 0; i < index + 1; i++) { newArray[arrayOfNumbers.length - 1 - index + i] = arrayOfNumbers[i]; } for (int i = 0; i < arrayOfNumbers.length - (index + 1); i++) { newArray[i] = arrayOfNumbers[index + i + 1]; } for (int i = 0; i < arrayOfNumbers.length; i++) { arrayOfNumbers[i] = newArray[i]; } } }