metalni

APS 2. Kolokvium 21/01/2021

Jan 21st, 2021 (edited)
616
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.Map;
  5.  
  6. class SLLNode<E> {
  7.     protected E element;
  8.     protected SLLNode<E> succ;
  9.  
  10.     public SLLNode(E elem, SLLNode<E> succ) {
  11.         this.element = elem;
  12.         this.succ = succ;
  13.     }
  14.  
  15.     @Override
  16.     public String toString() {
  17.         return element.toString();
  18.     }
  19. }
  20.  
  21. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  22.  
  23.     // Each MapEntry object is a pair consisting of a key (a Comparable
  24.     // object) and a value (an arbitrary object).
  25.     K key;
  26.     E value;
  27.  
  28.     public MapEntry (K key, E val) {
  29.         this.key = key;
  30.         this.value = val;
  31.     }
  32.  
  33.     public int compareTo (K that) {
  34.         // Compare this map entry to that map entry.
  35.         @SuppressWarnings("unchecked")
  36.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  37.         return this.key.compareTo(other.key);
  38.     }
  39.  
  40.     public String toString () {
  41.         return "<" + key + "," + value + ">";
  42.     }
  43. }
  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 SLLNode<MapEntry<K, E>>[] getBuckets() {
  91.         return buckets;
  92.     }
  93.  
  94.     public void delete(K key) {
  95.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  96.         int b = hash(key);
  97.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  98.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  99.                 if (pred == null)
  100.                     buckets[b] = curr.succ;
  101.                 else
  102.                     pred.succ = curr.succ;
  103.                 return;
  104.             }
  105.         }
  106.  
  107.     }
  108.  
  109.     public String toString() {
  110.         String temp = "";
  111.         for (int i = 0; i < buckets.length; i++) {
  112.             temp += i + ":";
  113.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  114.                 temp += curr.element.toString() + " ";
  115.             }
  116.             temp += "\n";
  117.         }
  118.         return temp;
  119.     }
  120.  
  121. }
  122.  
  123. public class CoronaRiskFactor {
  124.     public static void main(String[] args) throws NumberFormatException, IOException {
  125.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  126.         int N = Integer.parseInt(bf.readLine());
  127.         //
  128.         CBHT<String, String> positive = new CBHT<>(2 * N);
  129.         CBHT<String, String> negative = new CBHT<>(2 * N);
  130.  
  131.         for(int i = 0; i<N; i++){
  132.             String p []= bf.readLine().split(" ");
  133.             String opstina = p[0];
  134.             String prezime = p[1];
  135.             String rezultat = p[2];
  136.  
  137.             if(rezultat.equals("pozitiven"))
  138.                 positive.insert(opstina, prezime);
  139.             else
  140.                 negative.insert(opstina, prezime);
  141.         }
  142.         String opstina = bf.readLine();
  143.  
  144.         //broenje pozitivni pacienti
  145.         int positiveCounter = 0;
  146.         if(positive.search(opstina) != null){
  147.             SLLNode<MapEntry<String, String>> curr = positive.search(opstina);
  148.             while(curr != null){
  149.                 positiveCounter++;
  150.                 curr = curr.succ;
  151.             }
  152.         }
  153.  
  154.         int negativeCounter = 0;
  155.         if(negative.search(opstina) != null){
  156.             SLLNode<MapEntry<String, String>> curr = negative.search(opstina);
  157.             while(curr != null){
  158.                 negativeCounter++;
  159.                 curr = curr.succ;
  160.             }
  161.         }
  162.  
  163.         float riskFactor =  (float) positiveCounter / (negativeCounter + positiveCounter);
  164.  
  165.         System.out.println(String.format("%.2f", riskFactor));
  166.     }
  167.  
  168.  
  169. }
  170.  
Add Comment
Please, Sign In to add comment