Advertisement
Mitrezzz

CoronaRiskFactorProba

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