Advertisement
196040

APS Vtor kolokvium

Jan 21st, 2021
756
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.13 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. class SLLNode<E> {
  5.     protected E element;
  6.     protected SLLNode<E> succ;
  7.     public SLLNode(E elem, SLLNode<E> succ) {
  8.         this.element = elem;
  9.         this.succ = succ;
  10.     }
  11.     @Override
  12.     public String toString() {
  13.         return element.toString();
  14.     }
  15. }
  16. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  17.     // Each MapEntry object is a pair consisting of a key (a Comparable
  18.     // object) and a value (an arbitrary object).
  19.     K key;
  20.     E value;
  21.     public MapEntry (K key, E val) {
  22.         this.key = key;
  23.         this.value = val;
  24.     }
  25.     public int compareTo (K that) {
  26.         // Compare this map entry to that map entry.
  27.         @SuppressWarnings("unchecked")
  28.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  29.         return this.key.compareTo(other.key);
  30.     }
  31.     public String toString () {
  32.         return "<" + key + "," + value + ">";
  33.     }
  34. }
  35. class CBHT<K extends Comparable<K>, E> {
  36.     // An object of class CBHT is a closed-bucket hash table, containing
  37.     // entries of class MapEntry.
  38.     private SLLNode<MapEntry<K,E>>[] buckets;
  39.     @SuppressWarnings("unchecked")
  40.     public CBHT(int m) {
  41.         // Construct an empty CBHT with m buckets.
  42.         buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  43.     }
  44.     private int hash(K key) {
  45.         // Translate key to an index of the array buckets.
  46.         return Math.abs(key.hashCode()) % buckets.length;
  47.     }
  48.     public SLLNode<MapEntry<K,E>> search(K targetKey) {
  49.         // Find which if any node of this CBHT contains an entry whose key is
  50.         // equal
  51.         // to targetKey. Return a link to that node (or null if there is none).
  52.         int b = hash(targetKey);
  53.         for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  54.             if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  55.                 return curr;
  56.         }
  57.         return null;
  58.     }
  59.     public void insert(K key, E val) {      // Insert the entry <key, val> into this CBHT.
  60.         MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  61.         int b = hash(key);
  62.         //    for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  63.         //       if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  64.         //          // Make newEntry replace the existing entry ...
  65.         //         curr.element = newEntry;
  66.         //        return;
  67.         //   }
  68.         // }
  69.         // Insert newEntry at the front of the 1WLL in bucket b ...
  70.         buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  71.     }
  72.     public void delete(K key) {
  73.         // Delete the entry (if any) whose key is equal to key from this CBHT.
  74.         int b = hash(key);
  75.         for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  76.             if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  77.                 if (pred == null)
  78.                     buckets[b] = curr.succ;
  79.                 else
  80.                     pred.succ = curr.succ;
  81.                 return;
  82.             }
  83.         }
  84.     }
  85.     public String toString() {
  86.         String temp = "";
  87.         for (int i = 0; i < buckets.length; i++) {
  88.             temp += i + ":";
  89.             for (SLLNode<MapEntry<K,E>> curr = buckets[i]; curr != null; curr = curr.succ) {
  90.                 temp += curr.element.toString() + " ";
  91.             }
  92.             temp += "\n";
  93.         }
  94.         return temp;
  95.     }
  96. }
  97. public class CoronaRiskFactor {
  98.     public static void main(String[] args) throws NumberFormatException, IOException {
  99.         BufferedReader bf = new BufferedReader(new InputStreamReader(System.in));
  100.         int N = Integer.parseInt(bf.readLine());
  101.         CBHT<String, String> pos = new CBHT<>(100);
  102.         CBHT<String, String> neg = new CBHT<>(100);
  103.         for(int i = 0; i<N; i++){
  104.             String p = bf.readLine();
  105.             String [] cut = p.split(" ");
  106.             String opstina = cut[0];
  107.             String prezime = cut[1];
  108.             String rezultat = cut[2];
  109.             if(rezultat.equals("pozitiven")){
  110.                 pos.insert(opstina, prezime);
  111.             }
  112.             if(rezultat.equals("negativen")) {
  113.                 neg.insert(opstina, prezime);
  114.             }
  115.         }
  116.         double poz = 0;
  117.         double negg = 0;
  118.         String opstina = bf.readLine();
  119.         for(int i=0; i<N; i++) {
  120.             SLLNode help = pos.search(opstina);
  121.             if(pos.search(opstina)!=null) {
  122.                 while(help!=null) {
  123.                     poz++;
  124.                     help = help.succ;
  125.                 }
  126.             }
  127.             help = neg.search(opstina);
  128.             if(neg.search(opstina)!=null) {
  129.                 while(help!=null) {
  130.                     negg++;
  131.                     help = help.succ;
  132.                 }
  133.             }
  134.         }
  135.         double riskFactor = 0.0;
  136.         if(negg+poz != 0.0)
  137.             riskFactor = (double) poz / (negg + poz);
  138.         System.out.println(String.format("%.2f", riskFactor));
  139.     }
  140. }
  141. /*
  142.     Поради ковид пандемијата при секое тестирање на даден пациент се зачувува општината во која живее,
  143.      неговото презиме и дали е позитивен или негативен на корoна вирусот.
  144.      Потребни се статистички податоци во коишто ќе се одреди ризик факторот за дадена општина.
  145.       За таа цел се чуваат две хеш табели (CBHT) едната за позитивни, а другата за негативни
  146.       пациенти од корона вирусот. Ваша задача е за дадена општина на излез да го испечатите
  147.       ризик факторот во дадената општина.
  148.         Ризик фактор = (број на позитивни пациенти)/(број на негативни пациенти+број на позитивни пациенти)
  149.         Забелешка: Можно е да се појават пациенти со исто презиме. Истите треба да се земат како
  150.         посебни вредности во статистиката.
  151.         Влез:
  152.         На влез најпрво е даден бројот на пациенти - N, а потоа секој пациент е даден во нов
  153.         ред во форматот:
  154.         “Општина во која живее”  “Презиме на пациент”  “Резултати од тестот(позитивен/негативен)”
  155.         На крај е дадена општината за која треба да се пресмета ризик факторот
  156.         Излез:
  157.         Децимален број заокружен на две децимали кој го претставува ризик факторот за дадената општина
  158.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement