veronikaaa86

game of names

Mar 5th, 2018
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.83 KB | None | 0 0
  1. package JavaSyntax;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class P11_GameOfNames {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.         int n = Integer.parseInt(scanner.nextLine());
  12.  
  13.         long maxValue = Long.MIN_VALUE;
  14.         String winnerName = "";
  15.         boolean isRightLength = false;
  16.         Map<String, Long> map = new HashMap<>();
  17.         for (int i = 0; i < n; i++) {
  18.             String name = scanner.nextLine();
  19.             long initialScore = Long.parseLong(scanner.nextLine());
  20.  
  21.             if (name.length() > 2 && name.length() < 31) {
  22.                 isRightLength = true;
  23.                 long copyScore = initialScore;
  24.                 for (int j = 0; j < name.length(); j++) {
  25.                     long charScore = name.charAt(j);
  26.                     if (charScore % 2 != 0) {
  27.                         copyScore -= charScore;
  28.                     } else {
  29.                         copyScore += charScore;
  30.                     }
  31.                 }
  32.  
  33.                 map.putIfAbsent(name, copyScore);
  34.  
  35.                 for (int j = 0; j < map.size(); j++) {
  36.                     if (map.get(name) > maxValue) {
  37.                         maxValue = map.get(name);
  38.                         winnerName = name;
  39.                     }
  40.                 }
  41.             }
  42.         }
  43. //        Map.Entry<String, Integer> max = map.entrySet()
  44. //                .stream()
  45. //                .sorted((e1, e2) -> e2.getValue().compareTo(e1.getValue()))
  46. //                .findFirst()
  47. //                .get();
  48. //
  49. //        System.out.printf("The winner is %s - %d points", max.getKey(), max.getValue());
  50.  
  51.         if (isRightLength) {
  52.             System.out.printf("The winner is %s - %d points", winnerName, maxValue);
  53.         }
  54.     }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment