Kame3

riskFactor_kolokviumska

Feb 7th, 2021 (edited)
457
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.37 KB | None | 0 0
  1. package com.example;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.IOException;
  5. import java.io.InputStreamReader;
  6. import java.util.HashMap;
  7. import java.util.Hashtable;
  8. import java.util.Map;
  9.  
  10. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  11.  
  12.     // Each MapEntry object is a pair consisting of a key (a Comparable
  13.     // object) and a value (an arbitrary object).
  14.     K key;
  15.     E value;
  16.  
  17.     public MapEntry (K key, E val) {
  18.         this.key = key;
  19.         this.value = val;
  20.     }
  21.  
  22.     public int compareTo (K that) {
  23.         // Compare this map entry to that map entry.
  24.         @SuppressWarnings("unchecked")
  25.         MapEntry<K,E> other = (MapEntry<K,E>) that;
  26.         return this.key.compareTo(other.key);
  27.     }
  28.  
  29.     public String toString () {
  30.         return "<" + key + "," + value + ">";
  31.     }
  32. }
  33.  
  34. class OBHT<K extends Comparable<K>,E> {
  35.  
  36.     // An object of class OBHT is an open-bucket hash table, containing entries
  37.     // of class MapEntry.
  38.     private MapEntry<K,E>[] buckets;
  39.  
  40.     // buckets[b] is null if bucket b has never been occupied.
  41.     // buckets[b] is former if bucket b is formerly-occupied
  42.     // by an entry that has since been deleted (and not yet replaced).
  43.  
  44.     static final int NONE = -1; // ... distinct from any bucket index.
  45.  
  46.     private static final MapEntry former = new MapEntry(null, null);
  47.     // This guarantees that, for any genuine entry e,
  48.     // e.key.equals(former.key) returns false.
  49.  
  50.     private int occupancy = 0;
  51.     // ... number of occupied or formerly-occupied buckets in this OBHT.
  52.  
  53.     @SuppressWarnings("unchecked")
  54.     public OBHT (int m) {
  55.         // Construct an empty OBHT with m buckets.
  56.         buckets = (MapEntry<K,E>[]) new MapEntry[m];
  57.     }
  58.  
  59.  
  60.     private int hash (K key) {
  61.         // Translate key to an index of the array buckets.
  62.         return Math.abs(key.hashCode()) % buckets.length;
  63.     }
  64.  
  65.  
  66.     public int search (K targetKey) {
  67.         // Find which if any bucket of this OBHT is occupied by an entry whose key
  68.         // is equal to targetKey. Return the index of that bucket.
  69.         int b = hash(targetKey); int n_search=0;
  70.         for (;;) {
  71.             MapEntry<K,E> oldEntry = buckets[b];
  72.             if (oldEntry == null)
  73.                 return NONE;
  74.             else if (targetKey.equals(oldEntry.key))
  75.                 return b;
  76.             else
  77.             {
  78.                 b = (b + 1) % buckets.length;
  79.                 n_search++;
  80.                 if(n_search==buckets.length)
  81.                     return NONE;
  82.  
  83.             }
  84.         }
  85.     }
  86.  
  87.  
  88.     public void insert (K key, E val) {
  89.         // Insert the entry <key, val> into this OBHT.
  90.         MapEntry<K,E> newEntry = new MapEntry<K,E>(key, val);
  91.         int b = hash(key); int n_search=0;
  92.         for (;;) {
  93.             MapEntry<K,E> oldEntry = buckets[b];
  94.             if (oldEntry == null) {
  95.                 if (++occupancy == buckets.length) {
  96.                     System.out.println("Hash tabelata e polna!!!");
  97.                 }
  98.                 buckets[b] = newEntry;
  99.                 return;
  100.             }
  101.             else if (oldEntry == former
  102.                     || key.equals(oldEntry.key)) {
  103.                 buckets[b] = newEntry;
  104.                 return;
  105.             }
  106.             else
  107.             {
  108.                 b = (b + 1) % buckets.length;
  109.                 n_search++;
  110.                 if(n_search==buckets.length)
  111.                     return;
  112.  
  113.             }
  114.         }
  115.     }
  116.  
  117.  
  118.     @SuppressWarnings("unchecked")
  119.     public void delete (K key) {
  120.         // Delete the entry (if any) whose key is equal to key from this OBHT.
  121.         int b = hash(key); int n_search=0;
  122.         for (;;) {
  123.             MapEntry<K,E> oldEntry = buckets[b];
  124.  
  125.             if (oldEntry == null)
  126.                 return;
  127.             else if (key.equals(oldEntry.key)) {
  128.                 buckets[b] = former;//(MapEntry<K,E>)former;
  129.                 return;
  130.             }
  131.             else{
  132.                 b = (b + 1) % buckets.length;
  133.                 n_search++;
  134.                 if(n_search==buckets.length)
  135.                     return;
  136.  
  137.             }
  138.         }
  139.     }
  140.  
  141.  
  142.     public String toString () {
  143.         String temp = "";
  144.         for (int i = 0; i < buckets.length; i++) {
  145.             temp += i + ":";
  146.             if (buckets[i] == null)
  147.                 temp += "\n";
  148.             else if (buckets[i] == former)
  149.                 temp += "former\n";
  150.             else
  151.                 temp += buckets[i] + "\n";
  152.         }
  153.         return temp;
  154.     }
  155.  
  156.  
  157.     public OBHT<K,E> clone () {
  158.         OBHT<K,E> copy = new OBHT<K,E>(buckets.length);
  159.         for (int i = 0; i < buckets.length; i++) {
  160.             MapEntry<K,E> e = buckets[i];
  161.             if (e != null && e != former)
  162.                 copy.buckets[i] = new MapEntry<K,E>(e.key, e.value);
  163.             else
  164.                 copy.buckets[i] = e;
  165.         }
  166.         return copy;
  167.     }
  168. }
  169.  
  170. class Patient {
  171.     String prezime;
  172.     String opstina;
  173.     String pozitiven;
  174.  
  175.     public Patient(){}
  176.     public Patient(String prezime, String opstina, String pozitiven) {
  177.         this.prezime = prezime;
  178.         this.opstina = opstina;
  179.         this.pozitiven = pozitiven;
  180.     }
  181.     public String getPrezime() {
  182.         return this.prezime;
  183.     }
  184.     public String getOpstina() {
  185.         return this.opstina;
  186.     }
  187.     public String getPozitiven() {
  188.         return this.pozitiven;
  189.     }
  190.     public void setPrezime(String prezime) {
  191.         this.prezime = prezime;
  192.     }
  193.     public void setOpstina(String opstina) {
  194.         this.opstina = opstina;
  195.     }
  196.     public void setPozitiven(String pozitiven) {
  197.         this.pozitiven = pozitiven;
  198.     }
  199. }
  200.  
  201. public class Main {
  202.     public static void main(String[] args) throws IOException {
  203.         BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
  204.  
  205.         int n = Integer.parseInt(br.readLine());
  206.         HashMap<Integer,Patient> patients = new HashMap<>(n);
  207.  
  208.         for (int i=0; i<n; i++) {
  209.             String pacient = br.readLine();
  210.             String[] pacienti = pacient.split(" ");
  211.             String prezime = pacienti[1];
  212.             String opstina = pacienti[0];
  213.             String pozitiven = pacienti[2];
  214.             Patient p = new Patient(prezime,opstina,pozitiven);
  215.             patients.put(i, p);
  216.         }
  217.  
  218.         String opshtina = br.readLine();
  219.         int VKP = 0;
  220.         int VKN = 0;
  221.         float riskFactor;
  222.  
  223.         for (Map.Entry<Integer,Patient> curr : patients.entrySet()) {
  224.             if (curr.getValue().opstina.equals(opshtina)) {
  225.                 if (curr.getValue().pozitiven.equals("pozitiven")) {
  226.                     VKP++;
  227.                 } else if (curr.getValue().pozitiven.equals("negativen")) {
  228.                     VKN++;
  229.                 }
  230.             }
  231.         }
  232.  
  233.         System.out.println(VKP);
  234.         System.out.println(VKN);
  235.  
  236.         riskFactor = (float) VKP / (VKP + VKN);
  237.  
  238.         System.out.println(String.format("%.2f", riskFactor));
  239.  
  240. //        for (int i=0; i<n; i++) {
  241. //            System.out.println(patients.get(i).prezime);
  242. //        }
  243.     }
  244. }
  245.  
  246.  
  247. // risk factor = vkupen broj na pozitivni / (zbir od vkupen broj pozitivni i negativi);
  248.  
  249. 5
  250. Centar Trajko pozitiven
  251. Centar Vladislav negativen
  252. Karpos Svetoslava pozitiven
  253. Centar Trajan negativen
  254. Karpos Darko pozitiven
  255. Centar
  256.  
  257. output:
  258. 0.33
  259.  
  260.  
  261.  
Add Comment
Please, Sign In to add comment