06. Winning Ticket Text Processing - More Exercise
import com.sun.source.tree.IfTree; import java.sql.SQLOutput; import java.util.Scanner; public class WinningTicket { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); String[] tickets = scanner.nextLine().split(", "); String[] winnerSymbol = {"@", "#", "$", "^"}; for (int i = 0; i < tickets.length; i++) { if (tickets[i].length() < 20) { System.out.println("invalid ticket"); continue; } String symbol = getSmbol(winnerSymbol, tickets[i]); if (symbol.equals("notFound")) { printNoMatch(tickets[i]); continue; } int length = getCount(symbol, tickets[i]); if (length < 6) { printNoMatch(tickets[i]); continue; } String jackpot = checkJacpot(length); // String ticket = "\"" + tickets[i] + "\""; // System.out.print("ticket \"" + tickets[i]); // System.out.println("\" - " + length + symbol + jackpot); // System.out.println(String.format("ticket \"%s\" - %d%s%s", tickets[i], length, symbol, jackpot)); System.out.println("ticket \"" + tickets[i] + "\" - " + length + symbol + jackpot); } } private static void printNoMatch(String ticket) { System.out.println(String.format("ticket \"%s\" - no match", ticket)); } private static String checkJacpot(int length) { if (length >= 10) { return " Jackpot!"; } return ""; } private static int getCount(String symbol, String ticket) { int half = ticket.length() / 2; if (ticket.length() % 2 == 1) { half = ticket.length() / 2 - 1; } String leftHalf = removeNonWinSymb(ticket.substring(0, half), symbol); String rightHalf = removeNonWinSymb(ticket.substring(half), symbol); return leftHalf.length() < rightHalf.length() ? leftHalf.length() : rightHalf.length(); } private static String removeNonWinSymb(String halfTicket, String symbol) { StringBuilder combination = new StringBuilder(); for (int i = 0; i < halfTicket.length(); i++) { String check = "" + ((char) (halfTicket.charAt(i))); if (symbol.equals(check)) { combination.append(symbol); } } return combination.toString(); } private static String getSmbol(String[] winnerSymbol, String ticket) { for (String s : winnerSymbol) { if (ticket.contains(s)) { return s; } } return "notFound"; } }
link : https://judge.softuni.bg/Contests/Practice/Index/1674#5
Здравейте, Може ли малко помощ. 50/100 но дъдж на IJ го нямам тоя проблем на единия нулев ми вкарва празни пространства покрай " \" " и не знам как да го реша това:
ticket "$$$$$$$$$$$$$$$$$$$$ " - 10$ Jackpot!
invalid ticket
ticket " th@@@@@@eemo@@@@@@ey" - 6@