Crazy

Merenja Alternative Solution

May 28th, 2018
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.19 KB | None | 0 0
  1. import java.util.HashMap;
  2. import java.util.Scanner;
  3.  
  4. class Merenja{
  5.  
  6.     double sum;
  7.     int counter;
  8.  
  9.     public Merenja(double n){
  10.         sum=n;
  11.         counter=1;
  12.     }
  13.  
  14.  
  15. }
  16.  
  17. public class Main {
  18.  
  19.     public static void main(String[] args) {
  20.  
  21.  
  22.         Scanner in = new Scanner(System.in);
  23.  
  24.         int brMerenja = Integer.parseInt(in.nextLine());
  25.  
  26.  
  27.         HashMap<String, Merenja> hashMap = new HashMap<>();
  28.  
  29.         for (int i=0;i<brMerenja;i++){
  30.  
  31.             String [] niza = in.nextLine().split(" ");
  32.             String opstina = niza[0];
  33.             double temperatura = Double.parseDouble(niza[1].replace(',','.'));
  34.  
  35.  
  36.             if (hashMap.containsKey(opstina)){
  37.                 hashMap.get(opstina).counter++;
  38.                 hashMap.get(opstina).sum+=temperatura;
  39.             }
  40.  
  41.             else {
  42.  
  43.                 hashMap.put(opstina, new Merenja(temperatura));
  44.             }
  45.  
  46.  
  47.         }
  48.  
  49.         String grad = in.next();
  50.  
  51.         System.out.println(hashMap.get(grad).sum/hashMap.get(grad).counter);
  52.  
  53.  
  54.     }
  55. }
  56.  
  57. /*
  58. 5
  59.         Karposh 5,2
  60.         Chair 3,11
  61.         Centar 2,23
  62.         Karposh 1,7
  63.         Karposh 2,67
  64.         Karposh
  65.         3.19*/
Advertisement
Add Comment
Please, Sign In to add comment