Latkoski

ПМ честици

Jan 23rd, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.38 KB | None | 0 0
  1. package zagadenost;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.lang.ref.PhantomReference;
  7.  
  8. import javax.management.openmbean.OpenDataException;
  9. import javax.swing.InputMap;
  10.  
  11. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  12.  
  13.     // Each MapEntry object is a pair consisting of a key (a Comparable
  14.     // object) and a value (an arbitrary object).
  15.     K key;
  16.     E value;
  17.  
  18.     public MapEntry (K key, E val) {
  19.         this.key = key;
  20.         this.value = val;
  21.     }
  22.    
  23.     public int compareTo (K that) {
  24.     // Compare this map entry to that map entry.
  25.         @SuppressWarnings("unchecked")
  26.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  27.         return this.key.compareTo(other.key);
  28.     }
  29.  
  30.     public String toString () {
  31.         return "<" + key + "," + value + ">";
  32.     }
  33. }
  34. class SLLNode<E> {
  35.     protected E element;
  36.     protected SLLNode<E> succ;
  37.  
  38.     public SLLNode(E elem, SLLNode<E> succ) {
  39.         this.element = elem;
  40.         this.succ = succ;
  41.     }
  42.  
  43.     @Override
  44.     public String toString() {
  45.         return element.toString();
  46.     }
  47. }
  48.  
  49. class CBHT<K extends Comparable<K>, E> {
  50.  
  51.     // An object of class CBHT is a closed-bucket hash table, containing
  52.     // entries of class MapEntry.
  53.     private SLLNode<MapEntry<K,E>>[] buckets;
  54.  
  55.     @SuppressWarnings("unchecked")
  56.     public CBHT(int m) {
  57.         // Construct an empty CBHT with m buckets.
  58.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  59.     }
  60.  
  61.     private int hash(K key) {
  62.         // Translate key to an index of the array buckets.
  63.         return Math.abs(key.hashCode()) % buckets.length;
  64.     }
  65.  
  66.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  67.         // Find which if any node of this CBHT contains an entry whose key is
  68.         // equal
  69.         // to targetKey. Return a link to that node (or null if there is none).
  70.         int b = hash(targetKey);
  71.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  72.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  73.                 return curr;
  74.         }
  75.         return null;
  76.     }
  77.  
  78.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  79.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  80.         int b = hash(key);
  81.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  82.             if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  83.                 // Make newEntry replace the existing entry ...
  84.                 curr.element = newEntry;
  85.                 return;
  86.             }
  87.         }
  88.         // Insert newEntry at the front of the 1WLL in bucket b ...
  89.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  90.     }
  91.  
  92.     public void delete(K key) {
  93.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  94.         int b = hash(key);
  95.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  96.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  97.                 if (pred == null)
  98.                     buckets[b] = curr.succ;
  99.                 else
  100.                     pred.succ = curr.succ;
  101.                 return;
  102.             }
  103.         }
  104.     }
  105.  
  106.     public String toString() {
  107.         String temp = "";
  108.         for (int i = 0; i < buckets.length; i++) {
  109.             temp += i + ":";
  110.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  111.                 temp += curr.element.toString() + " ";
  112.             }
  113.             temp += "\n";
  114.         }
  115.         return temp;
  116.     }
  117. }
  118.  
  119.  
  120.  
  121. public class zagadenost {
  122.     public static void main(String[] args) throws NumberFormatException, IOException
  123.     {
  124.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  125.         int N = Integer.parseInt(br.readLine());//broj na opstini
  126.         int m = (int)(N*1.5);
  127.         CBHT<String,Double>table = new CBHT<String,Double>(m);
  128.         CBHT<String,Integer> table2 = new CBHT<String,Integer>(m);
  129.         double suma = 0;
  130.         for(int i = 0 ; i < N ; i++)
  131.         {
  132.             String string = br.readLine();
  133.             String[]podeleno = string.split(" ");
  134.             String opstina = podeleno[0];
  135.             int brojac =0;
  136.             double zagadenost = Double.parseDouble(podeleno[1]);
  137.             SLLNode<MapEntry<String,Double>> node = table.search(opstina);
  138.             SLLNode<MapEntry<String,Integer>> node2 = table2.search(opstina);
  139.             if(node == null){
  140.                 table.insert(opstina, zagadenost);
  141.                 table2.insert(opstina,1);
  142.             }
  143.             else
  144.             {
  145.                 brojac = node2.element.value+1;
  146.                 suma = zagadenost + node.element.value;
  147.                 table.delete(opstina);
  148.                 table.insert(opstina,suma);
  149.                 table2.insert(opstina, brojac);
  150.             }
  151.         }  
  152.         String opstina_nova = br.readLine();
  153.         SLLNode<MapEntry<String,Double>> node_prva = table.search(opstina_nova);
  154.         SLLNode<MapEntry<String,Integer>> node_vtora = table2.search(opstina_nova);
  155.         double prosek =0;
  156.         prosek = node_prva.element.value / node_vtora.element.value;
  157.         System.out.println(prosek);    
  158.     }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment