Latkoski

Загаденост

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