Advertisement
Mitrezzz

CoronaRiskFactor

Jan 24th, 2021
218
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. //kolokvium2 v1 CoronaRiskFactor - ne e celosna
  2.  
  3. /*
  4.  
  5. import java.io.BufferedReader;
  6. import java.io.IOException;
  7. import java.io.InputStreamReader;
  8.  
  9. class SLLNode<E> {
  10. protected E element;
  11. protected SLLNode<E> succ;
  12.  
  13. public SLLNode(E elem, SLLNode<E> succ) {
  14. this.element = elem;
  15. this.succ = succ;
  16. }
  17.  
  18. @Override
  19. public String toString() {
  20. return element.toString();
  21. }
  22. }
  23.  
  24. class MapEntry<K extends Comparable<K>,E> implements Comparable<K> {
  25.  
  26. // Each MapEntry object is a pair consisting of a key (a Comparable
  27. // object) and a value (an arbitrary object).
  28. K key;
  29. E value;
  30.  
  31. public MapEntry (K key, E val) {
  32. this.key = key;
  33. this.value = val;
  34. }
  35.  
  36. public int compareTo (K that) {
  37. // Compare this map entry to that map entry.
  38. @SuppressWarnings("unchecked")
  39. MapEntry<K,E> other = (MapEntry<K,E>) that;
  40. return this.key.compareTo(other.key);
  41. }
  42.  
  43. public String toString () {
  44. return "<" + key + "," + value + ">";
  45. }
  46. }
  47.  
  48.  
  49.  
  50. class CBHT<K extends Comparable<K>, E> {
  51.  
  52. // An object of class CBHT is a closed-bucket hash table, containing
  53. // entries of class MapEntry.
  54. private SLLNode<MapEntry<K,E>>[] buckets;
  55.  
  56. @SuppressWarnings("unchecked")
  57. public CBHT(int m) {
  58. // Construct an empty CBHT with m buckets.
  59. buckets = (SLLNode<MapEntry<K,E>>[]) new SLLNode[m];
  60. }
  61.  
  62. private int hash(K key) {
  63. // Translate key to an index of the array buckets.
  64. return Math.abs(key.hashCode()) % buckets.length;
  65. }
  66.  
  67. public SLLNode<MapEntry<K,E>> search(K targetKey) {
  68. // Find which if any node of this CBHT contains an entry whose key is
  69. // equal
  70. // to targetKey. Return a link to that node (or null if there is none).
  71. int b = hash(targetKey);
  72. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  73. if (targetKey.equals(((MapEntry<K, E>) curr.element).key))
  74. return curr;
  75. }
  76. return null;
  77. }
  78.  
  79. public void insert(K key, E val) { // Insert the entry <key, val> into this CBHT.
  80. MapEntry<K, E> newEntry = new MapEntry<K, E>(key, val);
  81. int b = hash(key);
  82. for (SLLNode<MapEntry<K,E>> curr = buckets[b]; curr != null; curr = curr.succ) {
  83. */
  84. /* if (key.equals(((MapEntry<K, E>) curr.element).key)) {
  85. // Make newEntry replace the existing entry ...
  86. curr.element = newEntry;
  87. return;
  88. }*//*
  89.  
  90. }
  91. // Insert newEntry at the front of the 1WLL in bucket b ...
  92. buckets[b] = new SLLNode<MapEntry<K,E>>(newEntry, buckets[b]);
  93. }
  94.  
  95. public void delete(K key) {
  96. // Delete the entry (if any) whose key is equal to key from this CBHT.
  97. int b = hash(key);
  98. for (SLLNode<MapEntry<K,E>> pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {
  99. if (key.equals(((MapEntry<K,E>) curr.element).key)) {
  100. if (pred == null)
  101. buckets[b] = curr.succ;
  102. else
  103. pred.succ = curr.succ;
  104. return;
  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 br = new BufferedReader(new InputStreamReader(System.in));
  126. int N = Integer.parseInt(br.readLine());
  127.  
  128. CBHT<String,Covek> table = new CBHT<>(2*N+1);
  129.  
  130. for(int i = 0; i<N; i++) {
  131. String p[] = br.readLine().split(" ");
  132. String opstina = p[0];
  133. String prezime = p[1];
  134. String rezultat = p[2];
  135.  
  136. float br_pozitivni = 0, br_negativni = 0;
  137.  
  138.  
  139. SLLNode<MapEntry<String, Covek>> curr = table.search(opstina);
  140.  
  141. if (curr != null) {
  142. while(curr != null){
  143. if (curr.element.key.equals(opstina)) {
  144. if(rezultat.equalsIgnoreCase("pozitiven"){
  145. br_pozitivni++;
  146. }else {
  147. br_negativni++;
  148. }
  149. }
  150.  
  151. table.insert(opstina, new Covek(prezime, rezultat, br_pozitivni,br_negativni));
  152. curr = curr.succ;
  153. }
  154.  
  155. }else{
  156. if(rezultat.equalsIgnoreCase("pozitiven"){
  157. br_pozitivni++;
  158. }else {
  159. br_negativni++;
  160. }
  161. table.insert(opstina, new Covek(prezime, rezultat, br_pozitivni,br_negativni));
  162. }
  163. */
  164. /* }else{
  165. br_pozitivni = curr.element.value.zarazeni + br_pozitivni;
  166. br_negativni = curr.element.value.neZarazeni + br_negativni;
  167. table.insert(opstina, new Covek(prezime, rezultat, br_pozitivni,br_negativni));
  168. }*//*
  169.  
  170.  
  171.  
  172.  
  173.  
  174. }
  175.  
  176. String opstina = br.readLine();
  177.  
  178. SLLNode<MapEntry<String, Covek>> curr = table.search(opstina);
  179. float zarazeni = curr.element.value.zarazeni;
  180. float neZarazeni = curr.element.value.neZarazeni;
  181.  
  182. float riskFactor = zarazeni / (neZarazeni+zarazeni);
  183.  
  184. System.out.println(String.format("%.2f", riskFactor));
  185. }
  186.  
  187.  
  188. }
  189.  
  190. class Covek{
  191.  
  192.  
  193. String prezime;
  194. String rezultat;
  195. float zarazeni;
  196. float neZarazeni;
  197.  
  198. public Covek(String prezime, String rezultat, float zarazeni, float neZarazeni) {
  199. this.prezime = prezime;
  200. this.rezultat = rezultat;
  201. this.zarazeni = zarazeni;
  202. this.neZarazeni = neZarazeni;
  203. }
  204. }
  205.  
  206.  
  207.  
  208. */
  209.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement