Advertisement
Guest User

Internships

a guest
Jan 16th, 2020
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.47 KB | None | 0 0
  1. import java.util.ArrayDeque;
  2. import java.util.Scanner;
  3. import java.util.regex.Matcher;
  4. import java.util.regex.Pattern;
  5.  
  6. public class Internships {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         int countTask = Integer.parseInt(scanner.nextLine());
  10.         int countPerson = Integer.parseInt(scanner.nextLine());
  11.  
  12.         String regex = "^[A-Z][a-z]+ [A-Z][a-z]+$";
  13.  
  14.         Pattern pattern = Pattern.compile(regex);
  15.  
  16.         ArrayDeque<String> tasks = new ArrayDeque<>();
  17.         ArrayDeque<String> names = new ArrayDeque<>();
  18.  
  19.         for (int i = 0; i < countTask; i++) {
  20.             String current = scanner.nextLine().trim();
  21.             tasks.push(current);
  22.         }
  23.  
  24.         for (int i = 0; i < countPerson; i++) {
  25.             String current = scanner.nextLine();
  26.             Matcher matcher = pattern.matcher(current);
  27.  
  28.             if (matcher.find()) {
  29.                 names.addLast(current);
  30.             }
  31.  
  32.         }
  33.  
  34.         // todo MOST IMPORTANT PART-------------
  35.  
  36.         while (tasks.size() > 0 && names.size() > 1) {
  37.  
  38.             String problem = tasks.peek();
  39.             String[] currentTask = problem.split("");
  40.             int sumTask = 0;
  41.             for (int i = 0; i < currentTask.length; i++) {
  42.                 sumTask += currentTask[i].charAt(0);
  43.             }
  44.  
  45.             String name = names.pop();
  46.             String[] currentName = name.split("");
  47.             int sumName = 0;
  48.             for (int i = 0; i < currentName.length; i++) {
  49.                 sumName += currentName[i].charAt(0);
  50.             }
  51.  
  52.             if (sumName > sumTask) {
  53.                 tasks.pop();
  54.                 names.addLast(name);
  55.                 System.out.println(name + " solved " + problem + ".");
  56.             } else {
  57.  
  58.                 tasks.pop();
  59.                 tasks.addLast(problem);
  60.                 System.out.println(name + " failed " + problem + ".");
  61.  
  62.             }
  63.  
  64.         }
  65.  
  66.         if (names.size() == 1) {
  67.             System.out.println(names.peek() + " gets the job!");
  68.         } else {
  69.  
  70. //            for (int i = 0; i < names.size(); i++) {
  71. //                if (i == names.size() - 1) {
  72. //                    System.out.println(names.pop());
  73. //                } else {
  74. //                    System.out.print(names.pop() + ", ");
  75. //                }
  76. //                i--;
  77. //            }
  78.             System.out.println(String.join(", ", names));
  79.  
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement