Advertisement
YavorGrancharov

Sums_by_Town

Dec 2nd, 2017
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.87 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Sums_by_Town {
  4.     public static void main(String[] args) {
  5.         Scanner console = new Scanner(System.in);
  6.  
  7.         Map<String, Double> map = new HashMap<>();
  8.  
  9.         int n = Integer.parseInt(console.nextLine());
  10.         for (int i = 0; i < n; i++) {
  11.             String line = console.nextLine();
  12.             String[] tokens = line.split("\\s+\\|\\s+");
  13.             String town = tokens[0];
  14.             double income = Double.parseDouble(tokens[1]);
  15.  
  16.             if (map.containsKey(town)) {
  17.                 map.put(town,income + map.get(town));
  18.             }
  19.             else {
  20.                 map.put(town,income);
  21.             }
  22.         }
  23.  
  24.         Map<String, Double> tree = new TreeMap<>(map);
  25.  
  26.         for (String key : tree.keySet()) {
  27.             System.out.println(key + " -> " + tree.get(key));
  28.         }
  29.     }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement