Проблем с задача " Ad Astra".
Здравейте,
Примерните входове от задачата минават, но в "Djudje" повечето тестове не минават (дава 16/100). Пробвах с различни "regex" - a, но резултата е същия. Моля за съдействие. Кода е:
import java.util.LinkedHashMap; import java.util.Map; import java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ExamTask___AdAstra { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); //10:38 String input = scanner.nextLine(); Pattern pattern = Pattern.compile ("(#|\\|)(?<item>[A-Za-z ]+)\\1(?<termin>\\d+\\/\\d+\\/\\d{2})\\1(?<calorie>\\d+)"); // ((\#)|(\|))(?<item>(\w+)|((\w+)( )(\w+)))((\#)|(\|))(?<termin>(([0-2])([0-9])(\/)){2}(\d{2}))(((\|)|(\#))(?<calorie>(\d+))((\|)|(\#))) Matcher correctInput = pattern.matcher(input); String separateItem = ""; int calorieSeparateInput = 0; String date = ""; int sumCalories = 0; Map<String,Integer> itemAndCalories = new LinkedHashMap<>(); Map<String,String> itemAndDate = new LinkedHashMap<>(); while (correctInput.find()) { separateItem = correctInput.group("item"); calorieSeparateInput = Integer.parseInt(correctInput.group("calorie")); date = correctInput.group("termin"); itemAndCalories.put(separateItem,calorieSeparateInput); itemAndDate.put(separateItem,date); sumCalories +=calorieSeparateInput; } int limitDays = sumCalories/2000; // You have food to last you for: 9 days! // Item: Fish, Best before: 24/12/20, Nutrition: 8500 if (limitDays>0) { System.out.printf("You have food to last you for: %d days!%n", limitDays); for (Map.Entry<String, String> element : itemAndDate.entrySet()) { String itemName = element.getKey(); System.out.printf("Item: %s, Best before: %s, Nutrition: %d%n", element.getKey(),element.getValue(),itemAndCalories.get(itemName)); } } else { System.out.println("You have food to last you for: 0 days!"); } } }
Благодаря за съдействието.